LandscapeOrientation on start of didload method in objective c

心不动则不痛 提交于 2019-12-01 04:15:36

问题


I've made an iPad application,

It works fine when I load my application in portrait mode for first time, but when I load my application in landscape mode for the first time, it takes the coordinates of portrait mode only, because inside my didLoad method I give coordinates of portrait mode only.

Now need to give coordinates of landscape mode inside my didLoad method.

I tried this, inside my didLoad method

if (interfaceOrientation == UIInterfacePortraitmode ||
    interfaceOrientation == UIInterfaceUpsideDown)
{
    // do this....
}
else
{
    // do this....
}

but I am unable to write the condition for the if/else inside my didLoad method.

What should I do?


回答1:


-(void) viewWillAppear: (BOOL) animated {
       [super viewWillAppear: animated];
       [self adjustViewtForNewOrientation: self.interfaceOrientation];
}

-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration {
       [self adjustViewtForNewOrientation: interfaceOrientation];
}


- (void) adjustViewtForNewOrientation: (UIInterfaceOrientation) orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
         // Do some stuff
    } else {
         // Do some other stuff
    }

also call adjustViewtForNewOrientation in your ViewDidLaod() method,




回答2:


You can do handling as below -

-(void) viewWillAppear: (BOOL) animated {
       [super viewWillAppear: animated];
       [self updateLayoutForNewOrientation: self.interfaceOrientation];
}

-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration {
       [self updateLayoutForNewOrientation: interfaceOrientation];
}

and then finally custom method -

- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
         // Do some stuff
    } else {
         // Do some other stuff
    }

}




回答3:


I had similar issue with UIScrollView. I had it fixed by aligning the subviews as suggested here.

- (void)alignSubViews
{
    // Position all the content views at their respective page positions
    scrollView.contentSize = CGSizeMake(self.contentViews.count * scrollView.bounds.size.width,
                                        scrollView.bounds.size.height);
    NSUInteger i = 0;
    for (UIView *v in self.contentViews) {
        v.frame = CGRectMake(i * scrollView.bounds.size.width, 0,
                             scrollView.bounds.size.width, scrollView.bounds.size.height);
        i++;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Setup subviews and then align the views.

    [self alignSubViews];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
                                         duration:(NSTimeInterval)duration 
{
    [self alignSubViews];
    scrollView.contentOffset = CGPointMake(self.currentPage * scrollView.bounds.size.width, 0);
}


来源:https://stackoverflow.com/questions/8775911/landscapeorientation-on-start-of-didload-method-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!