首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIWindow addSubview位置

UIWindow addSubview位置
EN

Stack Overflow用户
提问于 2015-12-30 10:43:02
回答 1查看 1.5K关注 0票数 1

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

我怎么能这么做?

我的代码:

代码语言:javascript
复制
@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上添加我的按钮,因为我还需要在其他视图控制器上添加该按钮。

我正在使用按钮库。

UPDATE I通过添加约束来解决问题:

代码语言:javascript
复制
@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))
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-30 11:07:50

要在正确的位置上显示子视图(按钮),必须指定其约束或设置框架。当您将某些视图的子视图添加到另一个视图时,约束和位置不会被携带。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34527895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档