gpt4 book ai didi

ios - 将按钮放在屏幕的左下角

转载 作者:行者123 更新时间:2023-11-28 15:38:20 25 4
gpt4 key购买 nike

我有这个功能可以创建一个按钮,但即使我添加了一个 subview ,它也没有出现在屏幕上。我也想把它放在屏幕的左下角。我知道如何将它居中,但我无法弄清楚右下角和左下角按钮的位置

// will basically crete the login button
lazy var loginButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(red: 114, green: 206, blue: 236, alpha: 0)

button.setTitle("LOG IN", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)

return button
}()



//will setup constraints for the button
func setupLoginButton(){



}

最佳答案

你必须先给你的按钮一个位置。像这样:

let loginButton: UIButton = UIButton(frame: CGRect(100, 400, 100, 50))

CGRectMake 函数采用以下 4 个参数:

CGRect(CGFloat x, CGFloat y, CGFloat width, CGFloat height);

编辑

 // create button and position it
let button: UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))

// set all the other properties you did
button.backgroundColor = UIColor(red: 114, green: 206, blue: 236, alpha: 0)

button.setTitle("LOG IN", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
// add button to view
self.view.addSubview(button)

编辑 2

如果你想设置按钮类型,你必须先这样做,因为一旦创建了按钮,你就不能改变它,也不能设置按钮类型:

// create button and set button type
let button = UIButton(type: UIButtonType.System)
// position the button
button.frame = CGRect(x: 30, y: 30, width: 150, height: 150)
// set all the other properties you did
button.backgroundColor = UIColor(red: 114, green: 206, blue: 236, alpha: 0)

button.setTitle("LOG IN", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
// add button to view
self.view.addSubview(button)

关于ios - 将按钮放在屏幕的左下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44057179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com