在风景视图后返回肖像

时间:2014-05-25 07:09:42

标签: ios objective-c uiwebview

我的应用程序需要在所有视图的纵向方向上,以及一个需要支持纵向和lansdscape orietation的视图(UIWebview)。

要设置默认的纵向方向,我将其放在我的app delegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
if(flagOrientationAll == YES){
    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait;
}}

我已经设法使用此方法将我的webview安排为关于用户方向的纵向/横向(也可以从stackoverflow获取)

-(void)viewWillAppear:(BOOL)animated
{

AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
//NSLog(@"viewWillDisappear -- Start");
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = NO;
}

使用两个UIView呈现肖像和风景:

-(void)viewWillLayoutSubviews {
    BOOL layoutIsPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
    if (layoutIsPortrait) {
        self.view = self.pView;
    [self.pView addSubview:pwebView];
}else{
    self.view = self.lView;
    [self.lView addSubview:lwebView];
}}

然后使用此代码关闭webview:

- (IBAction)doneButtonTapped:(id)sender
{
   [self.navigationController popViewControllerAnimated:YES];
}

然而,当它在横向状态下消失时,前一个屏幕(调用webview的屏幕)以横向显示而不是默认(纵向)。 在webview解雇之后我怎么能以肖像形式出现呢?

感谢

2 个答案:

答案 0 :(得分:1)

您应该为每个viewController实施支持的方向

就像我写的here

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    //Choose your available orientation, you can also support more tipe using the symbol |
    //e.g. return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight)
    return (UIInterfaceOrientationMaskPortrait);
}

答案 1 :(得分:0)

Swift 3.0:
(添加到调用可观察环境的ViewController)

override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}