2015-12-30 95 views
1

我想在特定位置(右下角)的UIWindow上添加一个按钮。该按钮在视图上显示良好,但不在正确的位置。它总是出现在左上角(x:0,y:0)。UIWindow addSubview的位置

我该怎么做?

我的代码:

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

注:我想补充的一个UIWindow我的按钮,因为我需要对其他视图控制器按钮为好。

我正在使用this按钮库。


UPDATE 我通过添加约束解决了我的问题:

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

回答

1

要显示你必须指定其限制或设置框右侧位置的子视图(您的按钮)。当您将视图的子视图添加到另一个视图时,不会执行约束和位置。

+0

Thx的答复,我也试图添加约束到窗口,但它不起作用。你知道我该怎么做吗? –

+0

你已经解决了这个问题吗? – iyuna

+0

是的,我编辑了我的帖子。 Thx为您提供帮助。 –