这里我有一个问题,我有一个stackView它包含5个元素,它们被赋予了topAnchor,LEADED,bottom,traling和padding,它们有相同的宽度大小,我想改变SelectPhoto:UIButton的宽度,但是因为它在stackView中它没有改变,我如何改变或者调用一个数组(stackView)中的一个元素,并且改变? 事情应该是这样的:
创建元素:
let selectImageButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Select Photo", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 28, weight: .light)
button.backgroundColor = .white
button.setTitleColor(#colorLiteral(red: 0.4149784446, green: 0.4487785101, blue: 0.8248928785, alpha: 1), for: .normal)
return button
}()
let userNameTextField : CustomTextField = {
let textField = CustomTextField(padding: 16)
textField.placeholder = "Username"
textField.backgroundColor = .white
return textField
}()
let emailTextField : CustomTextField = {
let textField = CustomTextField(padding: 16)
textField.placeholder = "Email"
textField.keyboardType = .emailAddress
textField.backgroundColor = .white
return textField
}()
正在添加StackView:
lazy var stackView = UIStackView(arrangedSubviews: [
selectImageButton,
userNameTextField,
emailTextField,
passwordTextField,
registrationButton
])
约束条件:
fileprivate func addingSettingStackView(_ stackView: UIStackView) {
view.addSubview(stackView)
stackView.axis = .vertical
stackView.spacing = 8
stackView.anchor(top: nil, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor,padding: .init(top: 0, left: 50, bottom: 0, right: 50))
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 100).isActive = true
}
尝试在视图更改后调用self.view.LayoutifNeeded()
。
在StackView中添加一个视图,其中包含您的按钮作为子视图。 使用约束使按钮在视图中居中,并设置其宽度和高度定位点。