景观模式由程序化

时间:2014-03-13 06:00:42

标签: objective-c ios7

我正在做一个新项目,我需要横向和纵向视图,我已经设计了我的页面。肖像画很好但是如何在编程中设置风景。帮我个伙。

3 个答案:

答案 0 :(得分:1)

我正在使用viewDidLayoutSubviews:

-(void)viewDidLayoutSubviews
{
    CGRect bounds = self.view.bounds;
    if (bounds.size.width > bounds.size.height)
    {
        // landscape layout
        [self.myView setFrame:CGRectMake(10.0f, 80.0f, 330.0f, 318.0f)];
    }
    else
    {
        // portrait layout
        [[self.myView setFrame:CGRectMake(10.0f, 10.0f, 1500.0f, 318.0f)];
    }
}

对我来说很好。我假设您的应用会响应方向更改。

答案 1 :(得分:0)

阅读Supporting multiple interface orientations。此外,如果你确实想在iOS决定使用另一个时强制某种方向,那么公共API无法做到这一点。

答案 2 :(得分:0)

这对我有用:

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.firstViewAppear) {
        [self.navigationController setNavigationBarHidden:YES];
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        self.navigationController.view.transform = CGAffineTransformIdentity;
        self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*(90)/180.0);
        self.navigationController.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
        [UIView commitAnimations];
        self.firstViewAppear = NO;

    }
    self.view.frame = self.view.frame;
    [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}