gpt4 book ai didi

ios - 使用 setTranslatesAutoresizingMaskIntoConstraints(false) 在 UIView 中添加为 subview 时,不会调用 UIButton 操作

转载 作者:搜寻专家 更新时间:2023-10-31 22:36:00 25 4
gpt4 key购买 nike

我有一个以编程方式创建的 UIButton,为事件“UIControlEvents.TouchUpInside”添加了“按下”功能。但是当添加为 UIView 的 subview 时,不会调用“按下”方法。下面提供代码供您引用。但是,当我删除 setTranslatesAutoresizingMaskIntoConstraints(false) 时它会起作用。我需要使用此方法来自动调整布局大小。

var myView = UIView()
let orderBook = UIButton()

override func viewDidLoad() {
super.viewDidLoad()

myView.backgroundColor = UIColor.redColor()
myView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(myView)

let views1 = ["myView": myView]

var constV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myView(>=100)]-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views1)

var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:[myView(==100)]|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views1)

self.view.addConstraints(constH)
self.view.addConstraints(constV)

orderBook.setTitle("Order Book", forState: UIControlState.Normal)
orderBook.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
orderBook.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)

myView.addSubview(orderBook)
}

func pressed(sender: UIButton!) {
println("pressed")
}

override func viewDidLayoutSubviews() {
orderBook.frame = CGRectMake(0, 0, myView.frame.width, 100)
}

最佳答案

我知道我的回复有点晚了,但无论如何我都会回复那些仍然有这个问题,或者已经想出解决方法的人。

以编程方式将 UIButton 添加到 UIView 后无法与 UIButton 交互的原因是您没有向 UIButton 添加所有必要的约束。在我的特殊情况下,我没有添加宽度/高度约束。下面是我以编程方式创建的按钮示例,我将其添加到同样以编程方式创建的 UIView。请注意我添加到按钮的约束。

    var popupView = UIView()
popupView.setTranslatesAutoresizingMaskIntoConstraints(false)
popupView.backgroundColor = Color.fromHex("#FFFFFF", alpha: 1)
popupView.layer.cornerRadius = 20
popupView.clipsToBounds = true

var bottomView = UIView(frame: CGRectMake(0, 0, popupView.frame.size.width, 80))
bottomView.setTranslatesAutoresizingMaskIntoConstraints(false)

var singleBtn = UIButton()
singleBtn.titleLabel?.font = UIFont(name: "Nunito-Regular", size: 20)
singleBtn.setTitleColor(Color.fromHex("#979797", alpha: 1), forState: UIControlState.Normal)
singleBtn.setTitleColor(Color.fromHex("#56DAF0", alpha: 1), forState: UIControlState.Highlighted)

singleBtn.setTitle("OK", forState: UIControlState.Normal)
singleBtn.addTarget(self, action: "singleBtnAction:", forControlEvents: UIControlEvents.TouchUpInside)
singleBtn.setTranslatesAutoresizingMaskIntoConstraints(false)
singleBtn.backgroundColor = UIColor.redColor()
bottomView.addSubview(singleBtn)

bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
bottomView.addConstraint(NSLayoutConstraint(item: singleBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))

关于ios - 使用 setTranslatesAutoresizingMaskIntoConstraints(false) 在 UIView 中添加为 subview 时,不会调用 UIButton 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26857043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com