cuốn sách gpt4 ai đã làm

ios - 使用 insertSubview 显示时,UIViewController 在屏幕上被按下

In lại Tác giả: Walker 123 更新时间:2023-11-28 22:33:47 32 4
mua khóa gpt4 Nike

有点问题,这里是分割:

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

我遇到的问题是内容 View Controller 似乎在 rootViewController 中被向下推了一点。我为 rootViewController 设置了蓝色背景,为内容 View Controller 设置了橙色背景。这是我的代码:

AppDelegate.h

#import 

@class MainViewController;

@interface AppDelegate : UIResponder

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

@kết thúc

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 

@class ContentViewController;

@interface MainViewController : UIViewController

@property (nonatomic, retain) ContentViewController *content;

@kết thúc

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];
}

@kết thúc

ContentViewController.h

#import 

@interface LoginVC : UIViewController

@kết thúc

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];
}

@kết thúc

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

奇怪的是,如果我创建一个 View Controller 并使用 XIB 来绘制 View ,它看起来很好,内容 View 正好位于主视图的顶部。

如果有人能阐明这个问题,我将不胜感激。

chúc mừng

câu trả lời hay nhất

我想你不应该这样做

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];

关于ios - 使用 insertSubview 显示时,UIViewController 在屏幕上被按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16698495/

32 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress