UINavigationBar without UINavigationController

点点圈 提交于 2019-12-05 03:00:41
TangZijian

You can implement the delegate method -positionForBar: of the UINavigationBarDelegate protocol and simply return UIBarPositionTopAttached.

Example:

-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}

//if you're not using a `UINavigationController` and instead
//simply want a `UINavigationBar` then use the following method as well
-(void)testMethod
{
    UINavigationBar *navBar = [[UINavigationBar alloc] init];
    [navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    [navBar setBarTintColor:[UIColor lightGrayColor]];
    [navBar setDelegate:self];
    [self.view addSubview:navBar];
}

Hope this helps.

//Creating the plain Navigation Bar

UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.topItem.title = @"title";
[self.view addSubview:headerView];

You can't modify the status bar background color directly but since it is transparent (since iOS7), you can place a UIView of 20px in height behind the status bar with the desired color and it will appear as if the status bar has a background color.

As for the UINavigationBar, it's just modifying a UINavigationBar object that will help.

Example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //...

    //ISSUE 1: StatusBar Background
    UIView *vwStatusBarUnderlay = [[UIView alloc] init];
    [vwStatusBarUnderlay setFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    [vwStatusBarUnderlay setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:vwStatusBarUnderlay];
    //[vwStatusBarUnderlay sendSubviewToBack:self.view];

    //ISSUE 2: NavigationBar
    UINavigationBar *navBar = [[UINavigationBar alloc] init];
    [navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    [navBar setBarTintColor:[UIColor lightGrayColor]];
    [navBar setTranslucent:NO];

    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    [navItem setTitle:@"Favoriter"];
    [navItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]];

    [navBar setItems:@[navItem]];
    [self.view addSubview:navBar];
}

First answer worked well for me, but here it's in Swift instead of Objective-C:

1- you set the delegate of the navbar to the viewcontroller, whether via storyboard or the code.
2- add UINavigationBarDelegate to your controller protocols or extend it as follow:

extension ViewController: UINavigationBarDelegate {
    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }
}

That's it, works perfectly!

We can't set the color of the status bar by our choice, what we can do is;

In the AppDelegate.m file;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      //Set status bar style here
       [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!