Center UIPageControl after orientation change
I'm trying to centralize a UIPageControl
in portrait and landscape modes, but it isn't working, the x changes after device rotation.
@interface DetailViewController : UIViewController<UIScrollViewDelegate>
{
UIPageControl *pageControl;
}
- (void)viewDidLoad
{
pageControl = [[UIPageControl alloc] init] ;
[self renderMyView];
[self.view addSubview:pageControl];
}
- (void)renderMyView
{
if(isPortrait)
{
pageControl.frame = CGRectMake(200, 976, 0, 0);
} else {
pageControl.frame = CGRectMake(200, 720, 0, 0);
}
}
The renderMyView
is executed on didLoad
and didRotate
.
At first time viewDidLoad
works well in portrait and landscape, but if I rotate the device the pageControl
appears in a different x != 200
.
I've also tried pageControl.center
, but it didn't work.
How can I keep it c开发者_高级运维entralized?
1) In viewDidLoad
call renderMyView
.
2) After device has rotated don't call method renderMyView
3) Replace your renderMyView
with this one:
- (void)renderMyView
{
if(isPortrait)
{
pageControl.frame = CGRectMake(200, 976, 0, 0);
} else {
pageControl.frame = CGRectMake(200, 720, 0, 0);
}
pageControl.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
}
So, don't call my renderMyView
more then one time. AutoresizingMask
will do all you need.
Tell me, please, if it works for you.
PS: For more information about the autoresizingMask
, read the UIView Documentation.
probably a very old question, but I'll share my way to center the UIPageControl. Just set the width of the UIPageControl to be the same width as the view's bounds size width. So, In portrait mode it will be 768 and in the landscape it will be 1024. It works for me.
Set the UIView autoresizingMask in code:
pageControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
Or you can also do this in interface builder.
精彩评论