Reputation: 704
I have a view defined in a nib file. (named FooView.xib
)
The view is of a fixed size (smaller than screen size).
I would like to load it in a view controller (named FooViewController.[hm]
) and I want the view to show up in the center of the screen in whatever devices (a universal app).
Since the name of the xib and viewcontroller class have such correspondence, the view in the xib is loaded automatically.
Then I write code in the -(void)viewDidLoad
method to center my view, which can be referenced by self.view
. I write something like this.
CGRect screenBounds = [[UIScreen mainScreen]bounds];
self.view.center = CGPointMake(screenBounds.size.width/2, screenBounds.size.height/2);
And it does not work. Neither will it work when I modify self.view.frame = ...
.
I checked the values of self.view.center
or self.view.frame
. They did change but I didn't see any changes in the simulator.
Upvotes: 2
Views: 2774
Reputation: 17902
In the xib file make sure you have disabled auto-layout.
in viewController.m: (*Edit: place this in viewDidAppear not in viewDidLoad)
[self.view addSubview:myView];
myView.center = self.view.center;
Upvotes: 1