skip to Main Content

I create dialog with custom button UIButton in swift IOS. How can change UIButton size programmatically.

class SuccessMessageView: UIView, UITextFieldDelegate {

private var buttonConfirm : UIButton = {
    
    let button = UIButton()
    button.setTitle("Ok", for: .normal)
    button.backgroundColor = UIColor(rgb: PreferenceManager.shared.hasDevice() ? purpleColour : orangeColour)
    button.setTitleColor(.white, for: .normal)
    button.layer.cornerRadius = 10
    button.addTarget(self, action: #selector(confirmFunc), for: .touchDown)
    
    button.translatesAutoresizingMaskIntoConstraints = false
    button.widthAnchor.constraint(equalToConstant: 140.0).isActive = true
    button.frame.size.width = 140
    return button
}
}

3

Answers


  1. Chosen as BEST ANSWER

    I found the solution for set UIButton width and height programatically like that set with constant.

    button.anchor(nil, left: nil, bottom: nil, right: nil, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 20, heightConstant: 30)
        button.anchorCenterXToSuperview()
    

  2.  fileprivate func Frame() -> CGRect {
            var circleFrame = CGRect(x: 0, y: 0, width: 2, height: 2)
            return circleFrame
        }
    
    Login or Signup to reply.
  3. Could you try this? Just remove the frame-related lines.

    private lazy var buttonConfirm : UIButton = {
        let button = UIButton()
        button.setTitle("Ok", for: .normal)
        button.backgroundColor = .red
        button.setTitleColor(.white, for: .normal)
        button.layer.cornerRadius = 10
        button.addTarget(self, action: #selector(confirmFunc), for: .touchUpInside)
    
        button.translatesAutoresizingMaskIntoConstraints = false
        button.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
        button.heightAnchor.constraint(equalToConstant: 48).isActive = true
        return button
    }()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search