Reputation: 464
I'm trying to add a button on the UIWindow on a specific position (bottom right corner). The button appears well on the view but is not in the right position. It always appears at the top left corner (x: 0, y: 0).
How can I do that?
My code:
@IBOutlet var myButton: ANLongTapButton!
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
let titleString = "Title\n"
let hintString = "SubTitle"
let title = NSMutableAttributedString(string: titleString + hintString)
let titleAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSBackgroundColorAttributeName: UIColor.clearColor(), NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18)!]
let hitAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSBackgroundColorAttributeName: UIColor.clearColor(), NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 12)!]
title.setAttributes(titleAttributes, range: NSMakeRange(0, titleString.characters.count))
title.setAttributes(hitAttributes, range: NSMakeRange(titleString.characters.count, hintString.characters.count))
myButton.titleLabel?.lineBreakMode = .ByWordWrapping
myButton.titleLabel?.textAlignment = .Center
myButton.setAttributedTitle(title, forState: .Normal)
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
win.addSubview(myButton)
}
Note: I'm trying to add my button on the UIWindow because I need to have the button on the other view controllers as well.
I'm using this button library.
UPDATE I resolved my problem by adding constraints :
@IBOutlet var myButton: ANLongTapButton!
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
let titleString = "Title\n"
let hintString = "SubTitle"
let title = NSMutableAttributedString(string: titleString + hintString)
let titleAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSBackgroundColorAttributeName: UIColor.clearColor(), NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18)!]
let hitAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSBackgroundColorAttributeName: UIColor.clearColor(), NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 12)!]
title.setAttributes(titleAttributes, range: NSMakeRange(0, titleString.characters.count))
title.setAttributes(hitAttributes, range: NSMakeRange(titleString.characters.count, hintString.characters.count))
myButton.titleLabel?.lineBreakMode = .ByWordWrapping
myButton.titleLabel?.textAlignment = .Center
myButton.setAttributedTitle(title, forState: .Normal)
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
win.addSubview(myButton)
win.addConstraint(NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: win, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -10))
win.addConstraint(NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: win, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -10))
}
Upvotes: 1
Views: 1591
Reputation: 1797
To display subview (your button) on the right position you have to specify its constraints or set the frame. When you're adding some view's subview to another view, constraints and positions are not carried.
Upvotes: 1