我有一个在UINavigationController中使用的视图,并且会自动按下以适应窗口,如预期的那样。但是我需要提供与模型相同的视图,它没有UINavigationController(或需要一个),所以我只是添加一个UINavigationBar:
if (presentedAsModal == YES) {
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navigationBar];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonSystemItemDone target:nil action:@selector(dismissModalViewControllerAnimated:)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:NO];
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:0/255.0f green:180/255.0f blue:220/255.0f alpha:1.f]];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:0/255.0f green:140/255.0f blue:180/255.0f alpha:1.f]];
}
但是,navigationBar不会自动按下VC,最终会覆盖部分视图。是否有一种方法可以在添加navigationBar后使VC调整大小以适应新的可用空间(我不想手动执行)。
答案 0 :(得分:2)
您可以将vc嵌入导航控制器中以进行模态演示,或者做一些修改边界原点的技巧:
CGRect bounds = self.bounds;
bounds.origin.y = -44;
self.bounds = bounds;
...
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
...