将 UIView 添加到横向窗口

Posted

技术标签:

【中文标题】将 UIView 添加到横向窗口【英文标题】:Add UIView to window in landscape 【发布时间】:2014-05-02 11:43:56 【问题描述】:

我的应用程序从 UISplitViewController 开始,然后我想在我的故事板上显示来自 UIViewController 的另一个 UIView。所以我在 -viewWillAppear 中调用了这个函数:

-(void)showSplashScreen

    UIView *splashView;
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];


        UINavigationController *splashVC = [self.storyboard instantiateViewControllerWithIdentifier:@"splashscreen"];

        splashView = splashVC.view;

        NSLog(@"height: %f", splashVC.view.frame.size.height);
        NSLog(@"width: %f", splashVC.view.frame.size.width);


        NSLog(@"self view height: %f", self.view.frame.size.height);
        NSLog(@"self view width: %f", self.view.frame.size.width);


    [appDelegate.window addSubview:splashView];


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),dispatch_get_main_queue(), ^
        [UIView transitionWithView:appDelegate.window
                          duration:1.00f
                           options:UIViewAnimationOptionCurveEaseOut
                        animations:^(void)
                            splashView.alpha = 0.0f;
                        
                        completion:^(BOOL finished)
                            [splashView removeFromSuperview];
                        ];
    );


UIView 始终以纵向模式显示。我将情节提要中的所有内容都设置为横向模式。该应用程序以横向启动。只允许横向。当然没有这样的功能: [self.storyboard instantiateViewControllerWithIdentifier:@"splashscreen" inPortrait: False]; 我猜对开发人员来说会很容易......

那么,有人有想法吗? (当然我可以添加启动图像,或者我可以在 UISplitViewController 之前加载 UIViewController ......)但它也应该像这样工作吗?

【问题讨论】:

【参考方案1】:
-(void)showSplash

    UIView *splashView;
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName: IS_IPAD ? @"Main_iPad" : @"Main_iPhone" bundle:nil];
    UINavigationController *splashVC = [storyboard instantiateViewControllerWithIdentifier:@"splashscreen"];
    splashView = splashVC.view;


    splashView.frame = IS_IPAD ? self.splitViewController.view.bounds : appDelegate.window.bounds;

    IS_IPAD ? [self.splitViewController.view addSubview:splashView] : [appDelegate.window addSubview:splashView];


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),dispatch_get_main_queue(), ^
        [UIView transitionWithView: IS_IPAD ? self.splitViewController.view : appDelegate.window
                          duration:1.00f
                           options:UIViewAnimationOptionCurveEaseOut
                        animations:^(void)
                            splashView.alpha = 0.0f;
                        
                        completion:^(BOOL finished)
                            [splashView removeFromSuperview];
                        ];
    );


【讨论】:

【参考方案2】:

这可能会对您有所帮助:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (interfaceOrientation) 
        case UIInterfaceOrientationLandscapeLeft:
            self.transform = CGAffineTransformMakeRotation(M_PI * 270.0 / 180.0);
            break;

        case UIInterfaceOrientationLandscapeRight:
            self.transform = CGAffineTransformMakeRotation(M_PI * 90.0 / 180.0);
            break;

        case UIInterfaceOrientationPortraitUpsideDown:
            self.transform = CGAffineTransformMakeRotation(M_PI * 180.0 / 180.0);
            break;

        default:
            break;
    
    [self setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

【讨论】:

以上是关于将 UIView 添加到横向窗口的主要内容,如果未能解决你的问题,请参考以下文章

UIView 子视图不会改变方向

iOS - 在窗口中获取视图的位置?

如何在 Xcode 4.1 Interface Builder 窗口中放大/缩小以查看多个 UIView

毕加索加载横向图像但不将纵向加载到标记的信息窗口中

PB的数据窗口能横向显示记录吗?

在目标 c 中的应用程序顶部窗口上添加 UIView 加载屏幕