ios - 如何更改自定义 NavigationItem 标题和背景颜色?

标签 ios uinavigationbar

之前我用这个代码使用了一个导航 Controller :

self.navigationItem.title = @"News";

现在我遇到的问题是我不再需要导航 Controller ,因为我正在使用页面 Controller 进行导航。现在我添加了一个导航栏,但它不再使用此代码更改标题。

还有我怎样才能改变背景颜色?

最佳答案

在 iOS 7 中使用 didFinishLaunchingWithOptions 编写以下代码

if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) 
{
     [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed: 4.0/255.0 green:173.0/255.0 blue:214.0/255.0 alpha:1.0f ]]; //// change background color of navigationBar
     [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; /// set backButton color of navigation bar
     [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; // set title color 
     [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];  /// set all barButton item color
     self.navController.navigationBar.translucent = NO;// set translucent NO
}

根据您的要求使用。

关于ios - 如何更改自定义 NavigationItem 标题和背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23430394/

相关文章:

ios - 在iOS中滚动没有导航 Controller 的Web View 时隐藏导航栏

ios - 使用 iOS 8 自定义键盘发送图像?

ios - 如何从 native ios 应用程序直播到 YouTube 帐户

iphone - 加载 View 时导航栏跳跃

ios - 尽管嵌入在 NavigationController 中,但 UINavigationBar 未显示

ios - iOS 中 UINavigationBar 中的 "Back"按钮对 View 有何作用?

ios - 找不到体系结构 i386 Xcode 6.4 的符号

ios - 使用新 Bundle Id 上传的应用程序未显示在仪表板上

iOS静态库编译错误-目录为空

uinavigationbar - iOS 11 导航栏中的搜索栏