In addition to what @Jasarien and @Brad have said, don’t forget that you can force auto-centering using the Autosizing springs and struts. Essentially (in Interface Builder) you click around until there are no Autosizing lines visible, like this:
alt text http://gallery.me.com/davedelong/100084/Screen-20shot-202010-03-26-20at-2010-49-18-20AM/web.jpg?ver=12696222220001
In code, you set the -[UIView autoresizingMask] to:
Objective C :
(UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin)
Swift 3.x :
[.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
If you do these, then the view stays centered automatically.
EDIT #1
Answering your edit, you can’t do:
self.view.center = self.view.superview.center;
Unless your view has been added as a subview of another view ([anotherView addSubview:self.view];). My guess is that self.view.superview is returning nil, and so you’re (luckily) getting {0,0} as the return value when trying to invoke center on nil.
The proper place to center your view would probably be in the viewWillAppear: method of the view controller. Once the view is about to appear, you know that it must have a superview (otherwise how else would it be about to appear?). You could safely set your view’s center there.