如何在Swift(UIKit)中获取UIButton框架X位置?

plicqrtu  于 2023-06-21  发布在  Swift
关注(0)|答案(2)|浏览(118)

我想让我的蓝色下划线开始和结束在红色框架内。所以,我试图得到X的位置,我的UIButton的起始位置。红色的那个(红色是bg的颜色)。我该怎么做?

我尝试过的事情:

promoButton.frame.origin.x

promoButton.bounds.origin.x

结果:0.0

promoButton.layer.position.x

这实际上移动了下划线,但它开始于“促销”的“P”,而不是红色框架。
谁能告诉我如何得到帧X位置?
先谢谢你。
更新:
这是我在故事板中的表述:
首先是这个选项卡视图

接下来,我有容器

然后促销按钮本身

我猜0.0是因为它在视图中。但是否可以从屏幕计算X位置呢?

lo8azlld

lo8azlld1#

你可以通过自动布局/约束来实现这一点,所以你不需要担心“框架”。
我们将像这样设置故事板-注意:我们不会在情节提要中添加“下划线视图”:

我给“容器视图”和按钮设置了不同的背景色,以便更容易看到框架。
我们将@IBOutlet连接到三个按钮:

@IBOutlet var btn1: UIButton!
@IBOutlet var btn2: UIButton!
@IBOutlet var btn3: UIButton!

我们将所有按钮的Touch Up Inside连接到同一个函数。
viewDidLoad()中,我们将创建并添加underlineView,并为其提供4.0Height约束。看起来你想让它在按钮下面8点左右,所以我们也会给它一个相对于第一个按钮底部的Top约束(哪个按钮并不重要,因为我们假设它们是相同的)。
现在,我们将添加两个类属性:

var underlineLeadingConstraint: NSLayoutConstraint!
var underlineWidthConstraint: NSLayoutConstraint!

同样在viewDidLoad()中,我们将创建这两个约束,并将它们设置为与btn1相关。
当我们点击一个按钮时,我们将:

  • 取消这两个约束
  • 重新使它们与点击按钮相关
  • 并使变化生动起来

@IBAction func btnTap(_sender:UIButton){//取消激活下划线视图约束underlineLeadingConstraint. isActive = false underlineWidthConstraint. isActive = false

// re-create the Leading and Width constraints
  //  related to the tapped button
  underlineLeadingConstraint = underlineView.leadingAnchor.constraint(equalTo: sender.leadingAnchor)
  underlineWidthConstraint = underlineView.widthAnchor.constraint(equalTo: sender.widthAnchor)

  // activate those new constraints
  underlineLeadingConstraint.isActive = true
  underlineWidthConstraint.isActive = true

  // if you want to animate it
  UIView.animate(withDuration: 0.3, animations: {
      self.view.layoutIfNeeded()
  })

}
下面是完整的控制器类:

class UnderlineViewController: UIViewController {
    
    @IBOutlet var btn1: UIButton!
    @IBOutlet var btn2: UIButton!
    @IBOutlet var btn3: UIButton!

    var underlineView: UIView!
    
    var underlineLeadingConstraint: NSLayoutConstraint!
    var underlineWidthConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        underlineView = UIView()
        underlineView.backgroundColor = .blue
        underlineView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(underlineView)
        
        // underlineView height is 4-points
        underlineView.heightAnchor.constraint(equalToConstant: 4.0).isActive = true
        
        // looks like you want it about 8-points below the button bottom?
        underlineView.topAnchor.constraint(equalTo: btn1.bottomAnchor, constant: 8.0).isActive = true
        
        // create Leading and Width constraints for the underlineView
        underlineLeadingConstraint = underlineView.leadingAnchor.constraint(equalTo: btn1.leadingAnchor)
        underlineWidthConstraint = underlineView.widthAnchor.constraint(equalTo: btn1.widthAnchor)
        // activate those constraints
        underlineLeadingConstraint.isActive = true
        underlineWidthConstraint.isActive = true
        
    }

    @IBAction func btnTap(_ sender: UIButton) {
        // de-activate the underline view constraints
        underlineLeadingConstraint.isActive = false
        underlineWidthConstraint.isActive = false
        
        // re-create the Leading and Width constraints
        //  related to the tapped button
        underlineLeadingConstraint = underlineView.leadingAnchor.constraint(equalTo: sender.leadingAnchor)
        underlineWidthConstraint = underlineView.widthAnchor.constraint(equalTo: sender.widthAnchor)
        
        // activate those new constraints
        underlineLeadingConstraint.isActive = true
        underlineWidthConstraint.isActive = true
        
        // if you want to animate it
        UIView.animate(withDuration: 0.3, animations: {
            self.view.layoutIfNeeded()
        })
    }
    
}

以下是它运行时的样子:

由于我们现在使用的是约束--而不是计算框架坐标--当“标签按钮”改变时,下划线的宽度和位置将自动更新。例如在设备旋转时。

hi3rlvi2

hi3rlvi22#

使用UIView上提供的convert(_:, to:)方法在坐标空间之间进行转换,例如:

let button = UIButton(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
let container = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let subcontainer = UIView(frame: CGRect(x: 150, y: 150, width: 250, height: 250))

container.addSubview(subcontainer)
subcontainer.addSubview(button)

let buttonFrameWithinContainer = button.convert(button.bounds, to: container) // returns { 170, 170, 50, 50 }

相关问题