自定义UI导航栏和按钮

时间:2015-12-24 04:34:31

标签: ios objective-c user-interface ios7 uikit

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

这是我用来改变导航栏颜色

[[UINavigationBar appearance] setBarTintColor:[UIColor]]

我想更改按钮颜色

这是一个例子

5 个答案:

答案 0 :(得分:1)

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

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

中的

现在要自定义导航栏设置和外观,您需要设置:

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

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

UIBarButtonItem *leftbtn;
UIBarButtonItem *rightbtn;

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

根据您的需要自定义barbuttonItems

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

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

希望它对你有所帮助

答案 1 :(得分:0)

使用您想要的颜色创建 1x1像素图像。然后将以下代码添加到 AppDelegate 类。

input.value

OR

在基础viewController类上编写以下代码,然后将此类作为所有其他视图控制器的超类。

UIImage *btnBgColor = [[UIImage imageNamed:@"backColor"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[[UIBarButtonItem appearance] setBackgroundImage: btnBgColor 
                                        forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];

答案 2 :(得分:0)

您可以使用下面的导航栏进行自定义。在Storyboard中,您可以选择更改颜色。

enter image description here

enter image description here

否则你可以把UIView定制。如下, enter image description here

答案 3 :(得分:0)

您可以在导航栏中添加自定义按钮,例如:

    UIButton *btnObj = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnObj setFrame:CGRectMake(0, 0, 60, 44)];
    [btnObj setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [btnObj setImage:[UIImage imageNamed:@"<button background image>"] forState:UIControlStateNormal];
    [btnObj addTarget:self action:@selector(btnObjClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *itemBtnObj = [[UIBarButtonItem alloc] initWithCustomView:btnObj];
    self.navigationItem.rightBarButtonItems = [[NSArray alloc]initWithObjects:itemBtnObj, nil];

答案 4 :(得分:0)

你可以从故事板本身做到。 拖动UINavigation Bar

enter image description here

使用图像或背景颜色自定义导航,为其提供阴影。

enter image description here

添加导航项

enter image description here

您也可以根据需要使用图像或颜色和图像以及位置来自定义它们。

enter image description here

要添加约束,请参阅我的ans。 Adding constraints to a UIBarButtonItem

希望它有所帮助..快乐编码.. :)

相关问题