Search code examples
iosswiftlayoutautolayoutstoryboard

how i can change element in stackView or get element and change


here I have this problem, I have a stackView it contains 5 elements, and they were given topAnchor, leading,bottom,traling and padding, they have the same width size, I want to change SelectPhoto : UIButton width but since it is in stackView it has not changed, how can I change or call an element in an array (stackview), and change? that's about how it should be :

enter image description here

creating elements :

    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
}()

adding StackView :

 lazy var stackView = UIStackView(arrangedSubviews: [
              selectImageButton,
              userNameTextField,
              emailTextField,
              passwordTextField,
              registrationButton
              ])

constraints :

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
  }

** when i call Anchor function for Select photo image **

mistake :
"<NSLayoutConstraint:0x6000034a30c0 UIButton:0x7ffa1d40a580'Photo'.bottom == RegistrationForGithub.CustomTextField:0x7ffa1e808800.top (active)>", "<NSLayoutConstraint:0x6000034a0730 'UISV-spacing' V:[UIButton:0x7ffa1d40a580'Photo']-(8)-[RegistrationForGithub.CustomTextField:0x7ffa1e808800] (active)>"


Solution

  • Try to call self.view.layoutIfNeeded() after view changing.