Home > Enterprise >  Change UIButton size programmatically in dialog in Swift
Change UIButton size programmatically in dialog in Swift

Time:06-16

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

CodePudding user response:

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

CodePudding user response:

 fileprivate func Frame() -> CGRect {
        var circleFrame = CGRect(x: 0, y: 0, width: 2, height: 2)
        return circleFrame
    }

CodePudding user response:

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()
  • Related