【问题标题】:Change the size of an UIButton on screen size of iPhone在 iPhone 的屏幕大小上更改 UIButton 的大小
【发布时间】:2015-05-20 15:41:57
【问题描述】:

我想更改启用自动布局功能的 UIButton 的大小(宽度、高度)。以下是关闭自动布局功能的代码。启用自动布局功能后,我应该做哪些更改才能使代码正常工作。

    println("height :  \(height)")
    println("width :  \(width)")
    if (height == 460)
    {
        btn1.frame = CGRectMake(22, 66, 40, 40)
        //btn1.sizeToFit()

        //btn1.frame = CGRectMake(200, 200, 100, 100)
        println("480")
        self.view.addSubview(btn1)
    }
    else if (height == 548)
    {
       btn1.frame = CGRectMake(22, 66, 60, 60)


        //btn1.frame.size.height = 65;
        //CGRect frame = btn1.frame;
        //btn1.frame.size.height = btn1.frame.size.height-20;
        //btn1.frame = CGRectMake(200, 200, 100, 100)
        println("548")
        //btn1.sizeToFit()
        //self.view.addSubview(btn1)
    }
    else
    {
        //btn1.sizeToFit()
        btn1.frame = CGRectMake(22, 66, 75, 75)
        //btn1.frame = CGRectMake(200, 200, 100, 100)
        println("647")
        self.view.addSubview(btn1)
    }

【问题讨论】:

标签: iphone swift uibutton autolayout


【解决方案1】:

您需要以编程方式添加约束。

// After initialize button
btn1.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubView(btn1)

// Left
self.view.addConstraint(NSLayoutConstraint(item: btn1, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 22))

// Top
self.view.addConstraint(NSLayoutConstraint(item: btn1, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 66))

// Width
btn1.addConstraint(NSLayoutConstraint(item: btn1, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 60))

// Height
btn1.addConstraint(NSLayoutConstraint(item: btn1, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 60))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    相关资源
    最近更新 更多