我需要在我的 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 以便它始终位于底角。 我附上了截图,这是它的样子。
请指导。
最佳答案
替换这个
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/