开发者

Orientation changes issues

I'm encountering some issues with orientation changes. Actually, I have two buttons and I want them stuck to each other and centered:

-------------------------------------------
|          | Button1 | Button2 |          |
-------------------------------------------

For this purpose I use the following method:

- (void)positionViews
{

    CGPoint center = self.view.center;

    CGRect frame = self.buttonScreens.frame;
    frame.origin.x = center.x - frame.size.width;
    self.buttonScreens.frame = frame;

    frame = self.buttonGroups.frame;
    frame.origin.x = center.x;
    self.buttonGroups.frame = frame;

}

It is called in viewDidLoad and didRotateFromInterfaceOrientation:, it works well when the orie开发者_JAVA技巧ntation is portrait, it works well when the orientation changes to landscape, but the buttons don't appear centered when the view is loaded when already in landscape orientation:

-------------------------------------------
|       | Button1 |  | Button2 |          |
-------------------------------------------

What is the problem? How can I solve it?


didRotateFromInterfaceOrientation is being called twice in case when view is is loaded in landscape orientation as if it was loaded in portrait and then rotated to landscape.

My solution was to add bool flag and set it to YES at the beginning of viewDidLoad and

if (self.firstRun == YES)
{  
    self.firstRun = NO;
    return;
}

in didRotateFromInterfaceOrientation.

This is happening because your nib file is set in portrait and will always load in portrait, triggering notification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜