1

I have a view that is used in a UINavigationController and and is automatically pushed down to fit the window as expected. However I need to present the same view as a Model and it does not have a UINavigationController (or need one) so I just add a UINavigationBar like so:

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

However, the navigationBar does not automatically push down the VC and it ends up covering up part of my View. is there a way to make the VC resize to fit the new available space once the navigationBar is added (I don't want to do it manually).

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 に答える