Standalone UINavigationBar?

2019-08-13 07:45发布

I am looking to create a bar with a couple of buttons at the top of the iPhone interface just under the statusBar, the controller is a simple UIViewController so this is a bar for buttons not for navigation. I have got things working but it feels a little clunky and I was wondering if I was doing it right and not missing any shortcuts.

  • There is no action / selector: set yet as I am just setting this up.
  • I will probably remove the NSArray convenience method and add an alloc/release.

Code:

- (void)loadView {
    // VIEW
    UIView *tempView = [[UIView alloc] initWithFrame:screenFrame];

    // NAV BAR
    UINavigationBar *tempNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [tempNavigationBar setBarStyle:UIBarStyleBlack];
    UIBarButtonItem *testButton = [[UIBarButtonItem alloc] initWithTitle:@"TEST" style:UIBarButtonItemStyleBordered target:self action:nil];

    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    [navItem setLeftBarButtonItem:testButton];
    [testButton release];

    [tempNavigationBar setItems:[NSArray arrayWithObject:navItem]];
    [tempView addSubview:tempNavigationBar];
    [tempNavigationBar release];
    [navItem release];

    [self setView:tempView];
    [tempView release];
}

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-13 08:28

That looks alright to me. An alternative would be to do this in Interface Builder, but that's a completely different matter.

If you intend to support autorotation, you may have to add some more code to set autoresize masks for your navigation bar so it expands correctly. Actually, I would use Interface Builder for this, because I'm big on visualizing my autoresize masks.

查看更多
倾城 Initia
3楼-- · 2019-08-13 08:38

Yeah the question repeats but why you don't preferred I.B.? As you don't need navigational functionality then you need to look into an alternate i.e. UIToolBar comes to mind. its usage is same as navigation bar as you need to setItems with NSArray as you are doing.

I hope you will succeed in finding your proper solution.

查看更多
登录 后发表回答