【问题标题】:UIViewController being pushed down on screen when displayed using insertSubview使用 insertSubview 显示时 UIViewController 在屏幕上被按下
【发布时间】:2013-05-22 17:51:53
【问题描述】:

有点问题,下面是细分:

我的 AppDelegate 使用视图控制器作为它的 rootViewController。这个视图控制器的目的是交换几个其他的视图控制器,称之为登录和主屏幕。为了简单起见,我已经从我将发布的代码中删除了所有交换代码,并让 rootViewController 简单地显示它的第一个内容视图控制器,比如登录。

我遇到的问题是内容视图控制器似乎在 rootViewController 内被向下推了一点。我给 rootViewController 一个蓝色背景,给内容视图控制器一个橙色背景。这是我的代码:

AppDelegate.h

#import <UIKit/UIKit.h>

@class MainViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) MainViewController *main;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate

@synthesize main;

- (void)dealloc
{
    [_window release];
    [main release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    MainViewController *mainVC = [[MainViewController alloc] init];
    self.main = mainVC;
    self.window.rootViewController = self.main;

    [self.window makeKeyAndVisible];

    return YES;
}

MainViewController.h

#import <UIKit/UIKit.h>

@class ContentViewController;

@interface MainViewController : UIViewController

@property (nonatomic, retain) ContentViewController *content;

@end

MainViewController.m

#import "MainViewController.h"
#import "ContentViewController.h"

@implementation MainViewController

@synthesize content;

- (id)init
{
self = [super init];
    if (self ) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    ContentViewController *viewController = [[ContentViewController alloc] init];
    self.content = viewController;
    [self.view insertSubview:self.content.view atIndex:0];              
}

- (void)dealloc {
    [content release];  
    [super dealloc];
}

@end

ContentViewController.h

#import <UIKit/UIKit.h>

@interface LoginVC : UIViewController

@end

ContentViewController.m

#import "ContentViewController.h"

@implementation ContentViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor orangeColor];
}

- (void)dealloc {
    [super dealloc];
}

@end

上面的代码给了我一个状态栏,它下面有一个蓝色条(大约是状态栏的高度)(这是主视图控制器),然后是橙色的内容视图控制器,占据了屏幕的其余部分。出于某种原因,主视图控制器似乎将内容视图控制器向下推了一点。

奇怪的是,如果我创建一个视图控制器并使用 XIB 来绘制视图,它看起来还不错,并且内容视图正好位于主视图的顶部。

如果有人能对这个问题有所了解,我将不胜感激。

干杯

【问题讨论】:

    标签: ios ipad uiviewcontroller xcode4


    【解决方案1】:

    我想你不应该这样做

    ContentViewController *viewController = [[ContentViewController alloc] init];
    self.content = viewController;
    [self.view insertSubview:self.content.view atIndex:0];
    

    而是:

    ContentViewController *viewController = [[ContentViewController alloc] init];
    self.content = viewController;
    [self presentViewController:self.content animated:YES completion:nil];
    

    【讨论】:

    • 怕不行,现在根本看不到内容视图控制器(橙色)。我最终得到一个警告:“警告:尝试在 上显示 ,其视图不在窗口层次结构中!”调查可能的原因。
    • 出现此警告是因为您在“viewDidLoad”方法中调用了“presentViewController...”。为了摆脱“viewDidAppear:”方法中的警告调用“presentViewController ...”。但是,我试图重现您的原始问题,但我没有得到您描述的相同行为。隐藏状态栏了吗?
    • 是的,从 viewDidAppear 调用上面的代码就可以了,测试它。无论您想从哪个自定义方法调用它都可以,以防该方法不是从 viewDidLoad 调用的。
    • 您可以在 viewDidLoad 中调用 'presentModalViewController:animated:' 并且应用程序确实不会崩溃,但是该方法已被弃用。现在我们必须使用 'presentViewController:animated:completion:' 并在 viewDidLoad 中调用该方法会导致应用程序崩溃。
    • 在 viewDidAppear 方法中调用 'presentViewController:animated:completion' 就可以显示第一个内容视图控制器。感谢那。然而新问题......删除第一个内容视图控制器并用不同的替换它。 '[self dismissViewControllerAnimated:NO completion:NULL]' 什么都不做。还尝试了“[self.content.view removeFromSuperview]”,它会使屏幕变黑,所以我假设它不仅删除了内容视图控制器(橙色背景),还删除了 rootViewController(蓝色背景)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2023-04-04
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多