I'm currently doing something like this:
startButton.frame = CGRectMake(0, 0, startButton.frame.width, startButton.frame.height)
in viewDidLoad. However this seems to make no difference.
What are the correct way of doing such thing?
Use this to change position if not usign AutoLayout,
var X_Position:CGFloat? = 50.0 //use your X position here
var Y_Position:CGFloat? = 50.0 //use your Y position here
startButton.frame = CGRectMake(X_Position, Y_Position, startButton.frame.width, startButton.frame.height)
Well, the best way to set the position of your button is to use constraints! Here are some of mine that I used :)
let storeBtnImg = UIImage(named: "StoreButton")
storeBtn.translatesAutoresizingMaskIntoConstraints = false
storeBtn.frame = CGRect(origin: CGPointMake(self.frame.width / 7.8, self.frame.height / 1.9), size: CGSize(width: self.frame.width / 10, height: self.frame.height / 10))
storeBtn.backgroundColor = nil
storeBtn.addTarget(self, action: "storeBtnAction", forControlEvents: .TouchUpInside)
storeBtn.setImage(storeBtnImg, forState: UIControlState.Normal)
self.view?.addSubview(storeBtn)
NSLayoutConstraint(item: storeBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: storeBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -75).active = true
The important parts are always remember to set .translatesAutoresizingMaskIntoConstraints = false
and to put .active = true at the end of your constraint.
Hope this helps, good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With