用自己的宽度约束按钮

问题描述 投票:0回答:1

确实为constrain而奋斗buttons

我想要做的就是将我的2 buttons设置为height35,并且他们的width应该是他们所需要的。现在看起来像这样(左右按钮):

enter image description here

这是我设置它们的方式:

let communityButton: UIButton = {
    let v = UIButton()
    v.setImage(UIImage(systemName: "person.3.fill"), for: .normal)
    v.tintColor = UIColor.darkCustom
    v.imageView?.contentMode = .scaleAspectFill
    v.contentHorizontalAlignment = .fill
    v.contentVerticalAlignment = .fill
    v.translatesAutoresizingMaskIntoConstraints = false
    v.addTarget(self, action: #selector(communityButtonTapped), for: .touchUpInside)
    return v
}()

let profileButton: UIButton = {
    let v = UIButton()
    v.setImage(UIImage(systemName: "person.fill"), for: .normal)
    v.tintColor = UIColor.darkCustom
    v.imageView?.contentMode = .scaleAspectFill
    v.contentHorizontalAlignment = .fill
    v.contentVerticalAlignment = .fill
    v.translatesAutoresizingMaskIntoConstraints = false
    v.addTarget(self, action: #selector(profileButtonTapped), for: .touchUpInside)
    return v
}()

约束:

//contrain communityButton
communityButton.centerYAnchor.constraint(equalTo: bottomBar.centerYAnchor),
communityButton.centerXAnchor.constraint(equalTo: bottomBar.centerXAnchor, constant: -view.frame.width/3.5),
communityButton.heightAnchor.constraint(equalToConstant: 35),

// constrain profileButton
profileButton.centerYAnchor.constraint(equalTo: bottomBar.centerYAnchor),
profileButton.centerXAnchor.constraint(equalTo: bottomBar.centerXAnchor, constant: view.frame.width/3.5),
profileButton.heightAnchor.constraint(equalToConstant: 35),

这里限制的正确方法是什么?

ios swift uibutton constraints
1个回答
0
投票

您可能需要让autoLayout知道您想要宽度灵活。您可以通过在两个按钮定义上添加以下行来做到这一点:-

// Replace "myButton" with your buttons name in your case "v"
 myButton.autoresizingMask = [.flexibleWidth] 
© www.soinside.com 2019 - 2024. All rights reserved.