UINavigationController介绍:9-UINavigationItem定制示例
本节的示例代码定制了一个导航栏,其中,左右两侧分别为一个文字按钮以及一个图片按钮,中间为文字描述。这是最常使用的导航栏分布样式之一。
准备工作
在进行定制之前,首先进行如下的准备工作:
新建一个工程,并导入一个按钮图片;
在Appdelegate.m中,添加如下的代码,用于创建一个导航栏对象。有关UINavigationItem定制的代码,在下方代码标注的预留位置添加.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController *navRootVC = [[UIViewController alloc] init];
navRootVC.view.backgroundColor = [UIColor grayColor];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:navRootVC];
//代码添加预留位置
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = navVC;
[self.window makeKeyAndVisible];
return YES;
}
定制左侧文字按钮
在导航栏的左侧,我们可以添加一个文字按钮。显示效果如下:
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftBtn)];
navRootVC.navigationItem.leftBarButtonItem = leftBarButtonItem;
定制右侧图片按钮
在导航栏的右侧,我们可以添加一个图片按钮。我们需要预先导入一张图片。显示效果如下:
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"write_comment"] style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBtn)];
navRootVC.navigationItem.rightBarButtonItem = rightBarButtonItem;
定制导航栏中间视图
导航栏的中部,一般情况下,会显示一个文字说明,可以通过设置title属性进行定制。
navRootVC.navigationItem.title = @"99iOS.com";
另外,我们还可以添加一个提示性的语句。
navRootVC.navigationItem.prompt = @"苹果iOS开发进阶之路";
修改导航栏的tintColor
我们发现,默认情况下,添加到导航栏左右两侧的UIBarButtonItem的颜色都是蓝色的,我们可以通过修改导航栏UINavigationBar的tintColor属性,来修改导航栏整体的元素渲染颜色。
navVC.navigationBar.tintColor = [UIColor redColor];
另外,假如我们希望右侧的图片显示其原始的颜色,可以参考下一章节的教程。
示例代码
https://github.com/99ios/9.2.9
文章发布时间为: October 28th , 2016 at 10:13 am
最后编辑时间为: September 13th , 2017 at 03:02 pm
本文由 99ios 创作,转载请注明出处
最后编辑时间为: September 13th , 2017 at 03:02 pm
本文由 99ios 创作,转载请注明出处