1

I'm trying to add a button programmatically as follows.

    lazy var myButton: UIButton = {
       let button = UIButton(type: .System)
       button.translatesAutoresizingMaskIntoConstraints = false
       button.backgroundColor = UIColor.blueColor()
       button.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)
       button.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
       return button
    }()

My intention was to create a button with view's width and height, half of view's height. And I also have LeftAnchor, RightAnchor, WidthAnchor, TopAnchor constraints to this button.

I have the following piece of code in my viewDidLoad()

view.addSubview(myButton)

when I try to run this code, I'm not able to see exactly what i want to have on my simulator. I would like to see button width = view' width and height of button = view height /2

instead I see small button on the left top of the simulator. How do i resolve this issue?

Thanks !

4
  • The behavior you describe suggests you have not defined the constraints, which, along with not posting them, appears to say the issue is there. Could you please post the code defining the constraints? Commented Apr 26, 2017 at 19:02
  • myButton.rightAnchor.constraintEqualToAnchor(view.rightAnchor).active = true myButton.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true myButton.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true myButton.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true This is my constraint code that i have written Commented Apr 26, 2017 at 20:34
  • That looks like Swift 2 code, as in Swift 3 it's isActive. Correct? Also, have you tried leading/trailing instead of left/right? Commented Apr 26, 2017 at 20:38
  • Additionally, but probably not helpful. I do things differently. I define my controls as simple "let" or "var" and further define them in viewDidLoad or wherever I should. (Otherwise, I subclass with a convenience init instead of making the control a property - I think that's the correct term for how you are doing it. One thing that may matter though is the "lazy" declaration. Have you tried (1) removing it, or (2) flat out defining things instead of making it a property? Commented Apr 26, 2017 at 20:41

1 Answer 1

1

A better bet would be to use AutoLayoutConstraints.

var myButtonHeight: NSLayoutConstraint

view.addSubview(myButton)
myButton.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
myButtonHeight = myButton.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5)
myButtonHeight.isActive = true

It's not clear from your comment if changing of the button height is onTouchDown, of if its a toggle as a result of onTouchUpInside, but either way

myButtonHeight.constant = (buttonShouldBeTall) ? 20 : 0
view.setNeedsLayout()

Please keep in mind you'll need to position the leading/trailing/centerX anchor and leading/trailing/centerY anchor as well, depending on where you need it to be

Sign up to request clarification or add additional context in comments.

1 Comment

i used this earlier. but my requirement is to change the height of the button when it is clicked. If i use autolayout, Xcode is not allowing me to do change the height of button because of the constraints. Any other idea?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.