自定义UI导航栏和按钮 [英] Customise UI Navigation Bar and button

查看:115
本文介绍了自定义UI导航栏和按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我是ios开发的新手,想知道如何自定义导航栏按钮

Hello I am new to ios development and want to know how to customise navigation bar buttons

这就是我用来改变导航栏颜色的方法

This is what i used to change navigation bar color

[[UINavigationBar appearance] setBarTintColor:[UIColor]]

我想更改按钮颜色

这是一个例子

Here is a exmaple

推荐答案

首先你需要设置导航栏,假设你的第一个屏幕是SpalshViewController你需要将启动画面设置为导航栏的根视图控制器和然后将导航栏设置为根视图控制器,如:

First you need to set navigation bar, assuming your first screen is SpalshViewController you need to set splash screen as a root view controller for navigation bar and then set navigation bar as root view controller like :

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SplashScreenVC *obj =[CustomUtility getViewController:@"SplashScreenVC"];
self.navigationController =[[UINavigationController alloc] initWithRootViewController:obj];
self.window.rootViewController =self.navigationController;

in didFinishLaunchingWithOptions

现在来定制你需要设置的导航栏设置和外观:

Now coming to customize navigation bar setting and appearance you need to set :

self.navigationController.navigationBar.translucent = NO;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"top_bg2.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:@"Lato-Regular" size:18]}];

现在设置导航栏的自定义栏按钮,您需要创建栏杆项目,如:

Now setting the custom bar buttons for navigation bar you need to create barbutton items like :

UIBarButtonItem *leftbtn;
UIBarButtonItem *rightbtn;

[self.navigationController.navigationItem setLeftBarButtonItem:leftbtn];
[self.navigationController.navigationItem setRightBarButtonItem:rightbtn];

自定义 barbuttonItems 根据您的需要

如果你想在导航栏中设置多个按钮,你可以添加如下:

if you want to set more than one buttons to the navigation bar you can add like:

UIBarButtonItem *leftbtn1;
UIBarButtonItem *leftbtn2;
UIBarButtonItem *rightbtn1;
UIBarButtonItem *rightbtn2;
[self.navigationController.navigationItem setLeftBarButtonItems:@[leftbtn1,leftbtn2]];
[self.navigationController.navigationItem setRightBarButtonItems:@[rightbtn1,rightbtn2]];

希望它对您有所帮助

这篇关于自定义UI导航栏和按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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