Ios 使用自动布局将子视图添加到UIButton

Ios 使用自动布局将子视图添加到UIButton,ios,swift,uibutton,autolayout,subclass,Ios,Swift,Uibutton,Autolayout,Subclass,我已经创建了UIButton的子类。此子类(BubbleBtn)负责向按钮添加UIView 添加的视图应为距顶部、左侧和右侧6点,同时跨越父按钮高度的一半 代码: 问题是,添加的UIView的宽度和高度大小不正确;在较大的按钮上,宽度和高度都会变短 如何将UIView添加到按钮,以便气泡视图以适当的大小渲染 截图如下: 您应该根据superview的边界和正确的 因此,当superview的框架改变时,气泡会调整其宽度。尝试为“视图内部”按钮添加约束 func addBubbleView() {

我已经创建了
UIButton
的子类。此子类(
BubbleBtn
)负责向按钮添加
UIView

添加的视图应为距顶部、左侧和右侧6点,同时跨越父按钮高度的一半

代码:

问题是,添加的
UIView
的宽度和高度大小不正确;在较大的按钮上,宽度和高度都会变短

如何将UIView添加到按钮,以便气泡视图以适当的大小渲染

截图如下:


您应该根据superview的
边界和正确的


因此,当superview的
框架改变时,
气泡会调整其宽度。

尝试为“视图内部”按钮添加约束

func addBubbleView() {
   let bubble = UIView()
    bubble.isUserInteractionEnabled = false
    bubble.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
    bubble.layer.cornerRadius = 10
    bubble.layer.zPosition = -1
    self.addSubview(bubble)
    bubble.translatesAutoresizingMaskIntoConstraints = false
    let views = [
        "bubble": bubble
    ]
    var constraints = [NSLayoutConstraint]()
    let vBtnConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|-6-[bubble]-6-|", options: .init(rawValue: 0), metrics: nil, views: views)
    let hBtnConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-6-[bubble]-6-|", options: .init(rawValue: 0), metrics: nil, views: views)
    constraints += vBtnConstraint
    constraints += hBtnConstraint
    NSLayoutConstraint.activate(constraints)
}

添加bubble.frame=self.bounds我喜欢使用VFL。vBtnConstraint的声明,有没有办法使垂直约束的动态宽度为按钮高度的“一半”?我认为您希望气泡的中心高度等于按钮高度的一半。将vBtnConstraint替换为:让vBtnConstraint=NSLayoutConstraint.constraints(使用VisualFormat:“V:|-(self.frame.height/4)-(bubble]-(self.frame.height/4)-|”,选项:.init(rawValue:0),度量值:nil,视图:views),使应用程序崩溃:libc++abi.dylib:以NSException(lldb)类型的未捕获异常终止Oops..添加以下内容:让vBtnConstraint=NSLayoutConstraint.constraints(使用VisualFormat:“V:|-\(self.frame.height/4)-[bubble]-\(self.frame.height/4)-|“、选项:.init(rawValue:0)、度量:nil、视图:视图)在代码之前漏掉了一条斜线啊!VFL中的字符串插值。我将您的代码稍微修改为:“V:|-6-[bubble]-(self.frame.height/2)-|”-它在某种程度上起作用,但是行“self.frame.height/2”仍然没有生成正确的值。我不知道我的约束或自动布局是否有问题。我只希望气泡的高度是按钮的一半,从顶部的6开始,到btn的一半高度结束。我会坚持下去的
let bubble = UIView(frame: CGRect(x: bubbleBuffer, y: bubbleBuffer,
    width: self.bounds.width - (bubbleBuffer * 2), 
    height: self.bounds.height - (2 * bubbleBuffer))
bubble.translatesAutoresizingMaskIntoConstraints = true
bubble.autoresizingMask = [ .flexibleWidth, .flexibleHeight ]
func addBubbleView() {
   let bubble = UIView()
    bubble.isUserInteractionEnabled = false
    bubble.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
    bubble.layer.cornerRadius = 10
    bubble.layer.zPosition = -1
    self.addSubview(bubble)
    bubble.translatesAutoresizingMaskIntoConstraints = false
    let views = [
        "bubble": bubble
    ]
    var constraints = [NSLayoutConstraint]()
    let vBtnConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|-6-[bubble]-6-|", options: .init(rawValue: 0), metrics: nil, views: views)
    let hBtnConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-6-[bubble]-6-|", options: .init(rawValue: 0), metrics: nil, views: views)
    constraints += vBtnConstraint
    constraints += hBtnConstraint
    NSLayoutConstraint.activate(constraints)
}