ios - Swift3 center UIbutton -


ok, have added button .swift storyboard, have added following code viewcontroller.swift

 @iboutlet weak var hellobutton: uibutton!  override func viewdidload() {     super.viewdidload()     // additional setup after loading view, typically nib.      hellobutton.frame.origin.x = view.frame.size.width / 2    } 

however when test on iphone 5s moves right of screen , not make centered. unsure need make button centered.

yes first helloworld app, , maybe on head, understand web programing, , understand make center automaticilly need have screen view width total , button width. need button divide screen view width 2.

for example in css this

#button{margin:0 auto;} 

but not html or css. it's swift3

try this:

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

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

also, variable name should lowercase, followed code convention


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -