ios - 如何在Swift 3.0 中设置动态创建按钮的框架

标签 ios swift dynamic uibutton

我需要在我的 iPhone 应用程序的底角放置一个圆形按钮。到目前为止,对于 iPhone 5,下面的代码是好的,但对于其他 iPhone,按钮位于中间。

let button = UIButton(type: .custom)
        button.frame = CGRect(x: 260, y: 500, width: 50, height: 50)
        button.layer.cornerRadius = 0.5 * button.bounds.size.width
        button.clipsToBounds = true
        button.setImage(UIImage(named:"dummy.jpg"), for: UIControlState.normal);
        button.addTarget(self, action: #selector(bankButtonPressed), for: .touchUpInside)
        view.addSubview(button)

那么如何动态设置按钮名气,使其适合所有型号的 iPhone。 我想要 button.frame = CGRect(x: 260, y: 500, width: 50, height: 50) - 动态设置它的 x 和 y 以便它始终位于底角。 我附上了截图,这是它的样子。

enter image description here

请指导。

最佳答案

替换这个

button.frame = CGRect(x: 260, y: 500, width: 50, height: 50)

button.frame = CGRect(x:  UIScreen.main.bounds.size.width - 70 , y:  UIScreen.main.bounds.size.height - 70 , width: 50, height: 50)

或者使用自动布局

button.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([

    button.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant:-20),

    button.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant:-20),
])

关于ios - 如何在Swift 3.0 中设置动态创建按钮的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539555/

相关文章:

ios - 计算适合固定空间的 UITableViewCells 的数量

swift - 容器 View 中以编程方式创建的标签不会针对文本展开

javascript - 如何进行响应式设计,使 H2 在 1 行 DIV 中获得最大字体大小?

ios - Swift 无法识别的选择器发送到实例分段控件

IOS UIMapView "legal"链接在剪辑边界之外时可点击吗?

ios - 使用 PromiseKit 时更新 Realm 对象时出现问题

ios - IOS 中 "self"引用什么时候应该弱?

algorithm - 是否有针对此类问题的特定算法?

带有图像的 iOS 推送通知,就像在 iMessage 中一样

ios - 看不到属性错误