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 !
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 = trueThis is my constraint code that i have written