1

我有一个在 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,它最终会覆盖我的部分视图。添加导航栏后,有没有办法让 VC 调整大小以适应新的可用空间(我不想手动执行)。

4

1 回答 1

2

您可以将 vc 嵌入到导航控制器中以进行模态演示,或者做一些修改边界原点的小技巧:

CGRect bounds = self.bounds;
bounds.origin.y = -44;
self.bounds = bounds;
...
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
...
于 2012-06-24T02:51:47.120 回答