var loginButton: UIButton
loginButton = UIButton()
loginButton.setTitle("Click", for: .normal)
loginButton.bounds = view.bounds
view.addSubview(loginButton)
var loginButton: UIButton
loginButton = UIButton()
loginButton.setTitle("Click", for: .normal)
loginButton.bounds = view.bounds
view.addSubview(loginButton)
I don't know what you're trying to accomplish and this is not a question about Swift language features and is therefore posted in the wrong forum.
But I do know you wouldn't normally assign to the bounds property. You could assign to the center or frame properties, if you set translatesAutoresizingMaskIntoConstraints = false, but that's a Bad Idea nowadays. I suggest you do some reading on Auto Layout for how to properly size and position views.
Could you explain what is not working ?
Anyway, you need to define the frame :
loginButton = UIButton(frame: CGRect(x: 10, y: 50, width: 100, height: 30))You should define an action as well.
loginButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)with an action defined as:
@objc func buttonAction(sender: UIButton!) {
print("Button Clicked")
}
Hi
func setupLoginButton(){
view.addSubview(loginView)
loginButton.setTitleColor(UIColor.green, for: .normal)
loginButton.translatesAutoresizingMaskIntoConstraints = false
loginButton.setTitle("Button Clicked", for: .normal)
loginButton.backgroundColor = UIColor.brown
loginButton.addTarget(self, action: #selector(handleLogin), for: .touchUpInside)
loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loginButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
loginButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
loginButton.heightAnchor.constraint(equalToConstant: 18).isActive = true
}This is my setup. However, my UIButton is not showing in the view.
Update, the app now crashes at the centerXAnchor breakpoint
Glad for you.
Don't forget to close this thread. And could you explain what you modified, that may help others.
I added loginView instead of loginButton to the view. Thanks for the help