没有UINavigationController的UINavigationBar [英] UINavigationBar without UINavigationController

查看:123
本文介绍了没有UINavigationController的UINavigationBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的应用程序顶部有一个 UINavigationBar ,就像在手机应用程序中一样,但我不想将它与 UINavigationController ,也就是说,我想将它用作独立对象。

I want to have a UINavigationBar in the top of my app, just like in the phone app for example, but I don't want to use it with a UINavigationController, that is, I want to use it as a "standalone" object.

将拥有的视图控制器 UINavigationBar 的视图位于 .xib 文件中。我试图从对象库拖动 UINavigationBar 的实例,它工作正常,但应用程序的状态栏仍为白色,而 UINavigationBar 是灰色的。我希望状态栏具有相同的灰色调,但不是视图的其余部分。

The view controller that will have the UINavigationBar has its view in a .xib file. I tried to drag an instance of UINavigationBar from the Object Library and it works fine, but the status bar of the app is still white while the UINavigationBar is gray. I want the status bar to have the same gray tone, but not the rest of the view.

为了告诉你我的意思,我有两张照片。第一个是手机应用程序。请注意,状态栏和导航栏是灰色的,但背景为白色。

To show you what I mean, I have two pictures. The first is the phone app. Notice that the status bar and the navigation bar is gray but the background is white.

以下图片来自我的应用程序,因为您可以看到状态栏是白色的(我希望它是灰色的)同样)。

The following picture is from my app, as you can see the status bar is white (I want it to be gray as well).

我还尝试在视图控制器中使用以下代码。

I have also tried to use the following code in the view controller.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Köp ProViva";
}

无论是否使用 UINavigationBar,我都试过了/ code>但要么没有显示导航栏,要么看起来和以前一样。

I tried it both with and without the UINavigationBar but either no navigation bar shows up or it looks the same as before.

如何添加UINavigationBar并拥有状态栏具有相同的颜色?

推荐答案

您可以实现委托方法 -positionForBar: UINavigationBarDelegate 协议,只需返回 UIBarPositionTopAttached

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

示例:

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

希望这有帮助。

这篇关于没有UINavigationController的UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆