我希望UINavigationBar在我的应用程序的顶部有一个,就像在手机应用程序中一样,但我不想将它与a一起使用UINavigationController,也就是说,我想将它用作"独立"对象.
视图控制器将UINavigationBar在.xib文件中具有其视图.我试图UINavigationBar从对象库拖动一个实例,它工作正常,但应用程序的状态栏仍为白色而UINavigationBar灰色.我希望状态栏具有相同的灰色调,但不是视图的其余部分.
为了告诉你我的意思,我有两张照片.首先是手机应用程序.请注意,状态栏和导航栏是灰色的,但背景为白色.

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

我还尝试在视图控制器中使用以下代码.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Köp ProViva";
}
Run Code Online (Sandbox Code Playgroud)
无论有没有,我都尝试了它,UINavigationBar但是没有导航条显示或看起来和以前一样.
如何添加UINavigationBar并使状态栏具有相同的颜色?
小智 16
您可以实现的委托方法-positionForBar:的的UINavigationBarDelegate协议,并简单地返回UIBarPositionTopAttached.
例:
-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
Run Code Online (Sandbox Code Playgroud)
//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];
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.