[英]Adding image to Navigation bar in xcode?
我在xib中添加了導航欄和導航項。 現在我需要在導航欄的左側添加圖像,但它沒有被添加。 這是我正在處理的代碼:
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
}
我無法理解為什么圖像沒有被添加。 我該如何添加它?
看起來這是一個老問題,接受了答案。 但是,我想補充一下:
至少在Xcode 5中, 如果您使用的是故事板,則可以通過將空視圖拖動到標題區域,然后將ImageView放在空視圖中,將圖像添加為navigationBar的標題。
以下屏幕截圖可以讓您了解它的外觀:
@implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect {
CGRect currentRect = CGRectMake(0,0,100,45);
UIImage *image = [UIImage imageNamed:@"ic_launcher.png"];
[image drawInRect:currentRect];
}
@end
UPDATE
只需在viewDidLoad:
方法中添加此代碼即可。
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];
self.navigationItem.rightBarButtonItem = item;
如果你想在NavigationBar的背景中直接設置圖像,那么使用波紋線...
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
也看到這個鏈接如何添加定制圖像按鈕到uinavigationbar-in-iphone-sdk
我希望這能幫到你......
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed:@"Your Image"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 10, 72, 19);
[self.navViewController.navigationBar addSubview:imageView];
}
如果要在導航欄中的特定點設置圖像,則可以創建視圖,然后在視圖中添加圖像,調整圖像框,然后將此視圖添加到導航欄。
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Default.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[view addSubview:image];
[self.navigationController.navigationBar addSubview:view];
如果您要從xib添加NavBar,那么只需為您的UINavigationBar
創建IBOutlet
,然后在xib中連接它
[myNavigationBar addSubview:view];
For Swift 4:
override func viewDidAppear(_ animated: Bool) {
// 1
let nav = self.navigationController?.navigationBar
// 2
nav?.barStyle = UIBarStyle.black
nav?.tintColor = UIColor.yellow
// 3
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .scaleAspectFit
// 4
let image = UIImage(named: "Apple_Swift_Logo")
imageView.image = image
// 5
navigationItem.titleView = imageView
}
嘗試設置imageView.frame也請交叉檢查圖像initailized正確
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
imageView.frame.origin.y = 5.0;
self.navigationItem.titleView = imageView;
[imageView release];
你的代碼看起來不錯。 但我不知道你為什么需要在xib中添加導航欄和項目。 如果您創建自己的視圖控制器派生自UIViewController,它包含導航欄。 您可以嘗試從xib中刪除自己添加的導航欄和項目,看看發生了什么。
如果要在基於視圖的應用程序的第一個視圖上顯示導航欄,則需要修改應用程序類代碼,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
// REMOVE THIS LINE !!!!!!!!!!!!!
//self.window.rootViewController = self.viewController;
// ADD BELOW TWO LINES !!!!!!!!!!!!!!!!!!!!!!!
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
我剛做了如下測試:
在viewcontroller.m中,添加以下行。
- (void)viewDidLoad {[super viewDidLoad]; UIImageView * view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)]; view.image = [UIImage imageNamed:@“info.png”]; self.navigationItem.titleView = view; }
現在又跑了。 您將看到一個帶有圖像標題的導航欄。
我希望這會對你有所幫助。 如果不工作意味着檢查你的圖像名稱是否正確。
UIImage *yourimage = [UIImage imageNamed: @"i_launcher.png"];
UIButton *buttonBarButton = [ UIButton buttonWithType:UIButtonTypeCustom];
buttonBarButton.frame = CGRectMake(0, 0,yourimage.size.width,yourimage.size.height);
[buttonBarButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[buttonBarButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:buttonBarButton] autorelease];
self.navigationItem.leftBarButtonItem = buttonBarButtonItem;
如果你希望添加動作意味着:
[buttonBarButton addTarget:self action:@selector(your function) forControlEvents:UIControlEventTouchUpInside];
要在移動到另一個控制器之間隱藏/顯示圖像,請執行此操作。 相應地調整CGRectMake尺寸。
將其添加到頭文件中:
@property (weak, nonatomic) UIImageView *navBarLogo;
將其添加到.m控制器文件中:
- (void)viewWillAppear:(BOOL)animated {
UIImage *image = [UIImage imageNamed:@"image_name.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(53, 7, 210, 30);
self.navBarLogo = imageView;
[self.navigationController.navigationBar addSubview:imageView];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar sendSubviewToBack:self.navBarLogo];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.