m0j1
m0j1

Reputation: 4267

XIB: Centering elements

I have an XIB file containing a simple activity indicator that I want to be vertically and horizontally centered. In XCode I can center it in w:compact and h:compact, but when I run it on the device its not centered (is offset). I have searched and I found suggestions on constraints or disabling auto layout, but when I do any of these my activity indicator is not shown in the view anymore.
I'd appreciate if you can help me with this. Thanks.

Upvotes: 3

Views: 3275

Answers (3)

chrisamanse
chrisamanse

Reputation: 4319

If you're setting constraints in w:compact h:compact only, then it would only show in iPhones in landscape (except iphone 6+). Try adding the constraints in w:any h:any, which is where your base layout should be. It should work with any screen size or orientation.

You can also Debug View Hierarchy during debugging if you can't see some of your views:

enter image description here

Upvotes: 1

Bista
Bista

Reputation: 7893

Add Activity indicator and select it and add constraints like in image.

Autolayout constaraints

P.S - Learning curve of auto layout is very tricky and require patience. The more you practice, more you improve. Read online tutorials, youth videos..Implement!

Upvotes: 9

cy SG
cy SG

Reputation: 31

if you use auto layout,you must add all constraints for the view. You must define the width,the height,the horizontal and vertical location of the view. Or you can just not use auto layout,set the frame of the view programatically like

  theView.frame.origin = CGPointMake(self.view.frame.width/2 - theView.frame.size.width/2,self.view.frame.size.height/2 - theView.frame.size.height/2)

Upvotes: 1

Related Questions