Set the Center of a Uibutton Programmatically - Swift

Set the center of a UIButton programmatically - SWIFT

Try .center property like

myButton.center = self.view.center

You can also specify x and y If you need.

myButton.center.x = self.view.center.x // for horizontal
myButton.center.y = self.view.center.y // for vertical

Center alignment UIButton xCODE programmatically

The problem was I had set up "attributed" to the title in interface builder. I just had to set it to "plain" and it's working now.

How to center UIButton programmatically in UIImageView?

Try to set translatesAutoresizingMaskIntoConstraints to false.

How to center UIButton in Swift?

For your place uibutton center of your view , update your cgrectmake as bleow..

CGRectMake((self.view.frame.size.width - 240) / 2, (self.view.frame.size.height - 40) / 2,240,40)  

or

You can add one line after your code

loginBtn.center = self.view.center

For SignUp Button :
 

signup.frame = loginBtn.bounds
signup.center = CGPointMake(loginBtn.center.x, loginBtn.center.y + loginBtn.frame.size.height + 10)

Center UIButton to Superview class programmatically (Swift 3)

First problem is, you are trying to add a constraint (which references other views) before adding your view as subview. You should add it as subview first, then create constraints. That will get rid of the crash.

addArrangedSubview(playButton)
playButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true

Also, I don't know what you are trying to achieve here, why you are using a UIStackView as container. Bu that constraint will probably cause trouble for you. You are adding your UIButton as "arranged subview", which means its constraints will be created automatically. You can activate some constraints like width if you configured your UIStackView accordingly (its distribution propery etc.). But centerX is problematic on a horizontal stack view. You can try centering your stack view inside its superview or instead of adding your uibutton as arrangedsubview, adding it as subview:

addSubview(playButton)

Swift3 center UIbutton

Try this:

    HelloButton.center.x = view.frame.width/2

Yours is not correct because the origin.x is the first point (top left) of the button view, not the center of the view, you can change it's background color to different color to see it

Also, the variable name should be lowerCase, followed by code convention

Make a UIButton programmatically in Swift

You're just missing the colon at the end of the selector name. Since pressed takes a parameter the colon must be there. Also your pressed function shouldn't be nested inside viewDidLoad.

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myFirstLabel = UILabel()
let myFirstButton = UIButton()
myFirstLabel.text = "I made a label on the screen #toogood4you"
myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)
myFirstLabel.textColor = .red
myFirstLabel.textAlignment = .center
myFirstLabel.numberOfLines = 5
myFirstLabel.frame = CGRect(x: 15, y: 54, width: 300, height: 500)
myFirstButton.setTitle("✸", for: .normal)
myFirstButton.setTitleColor(.blue, for: .normal)
myFirstButton.frame = CGRect(x: 15, y: -50, width: 300, height: 500)
myFirstButton.addTarget(self, action: #selector(pressed), for: .touchUpInside)
}

@objc func pressed() {
var alertView = UIAlertView()
alertView.addButtonWithTitle("Ok")
alertView.title = "title"
alertView.message = "message"
alertView.show()
}

EDIT: Updated to reflect best practices in Swift 2.2. #selector() should be used rather than a literal string which is deprecated.

How to center attributed title in UIButton

This is because the text is an attributed String, you will need to align the text after it has been set.

Solution for you will be:

aButton.setAttributedTitle(title, for: .normal)
aButton.titleLabel?.textAlignment = .center

By setting attributed String, instead of String, you are clearing all the button's text property.

Nicer solution will be to add alignment attribute into the attributed String



Related Topics



Leave a reply



Submit