xcode 更改导航项的背景并添加图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18460227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing background of navigation item and adding image
提问by Francis F
I have a Root View controller, I need to know about changing its blue background to an image or changing its color and also want to add an image to its right side. For example in the below image i need to change the background of text Root View Controller to another image or change its color and add a small icon on right side of the Root View Controller text. How should I go about it? I have gone through Change Navigation bar Background image on each navigationbut still having trouble implementing it.
我有一个根视图控制器,我需要知道如何将其蓝色背景更改为图像或更改其颜色,并且还想将图像添加到其右侧。例如在下图中,我需要将文本根视图控制器的背景更改为另一个图像或更改其颜色并在根视图控制器文本的右侧添加一个小图标。我该怎么办?我已经在每个导航上完成了更改导航栏背景图像,但仍然无法实现它。
AppDelegate.h
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
Viewcontroller.h
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
回答by Master Stroke
Try putting any of these codes into ViewWillAppear/ViewDidAppear/didFinishLaunchingWithOptions
(in case of appDelegate).
尝试将这些代码中的任何一个放入ViewWillAppear/ViewDidAppear/didFinishLaunchingWithOptions
(在 appDelegate 的情况下)。
1)navigationItem.titleView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"urtitlebar.png"]];
1)navigationItem.titleView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"urtitlebar.png"]];
OR
或者
2)
2)
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"urtitlebar.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[myView addSubview:image];
[self.navigationController.navigationBar addSubview:myView];
OR
或者
3)
3)
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"urtitlebar.png"];
[navBar setBackgroundImage:image];
EDIT:(in case you're using old version of SDK say 3.2.5)
编辑:(如果您使用的是旧版本的 SDK,请说 3.2.5)
Create the subview of UINavigationBar
and override the method called -drawRect:(CGRect)rect
with,
创建子视图UINavigationBar
并覆盖调用的方法 - drawRect:(CGRect)rect
with,
@implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *navBarImage;
image = [UIImage imageNamed: @"urNavigationBar.png"];
}
[navBarImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
@结尾
回答by Mayank Jain
Try This code in didFinishLaunchingWithOptions of AppDelegate.m for custom image on navigation bar.
在 AppDelegate.m 的 didFinishLaunchingWithOptions 中尝试此代码,用于导航栏上的自定义图像。
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"yourImageName.png"] forBarMetrics:UIBarMetricsDefault];
You can also add custom button as bar button.
您还可以将自定义按钮添加为条形按钮。
// create a custom button in viewDidLoad of your ViewController in which you want to place a custom bar button
UIButton *customBackBtn =[[UIButton alloc] init];
[customBackBtn setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
customBackBtn.frame = CGRectMake(100, 100, 52, 31);
// set this customized button as BarButton
UIBarButtonItem *backBtn =[[UIBarButtonItem alloc] initWithCustomView:customBackBtn];
[customBackBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = backBtn;
Thats It.
就是这样。