【问题标题】:Changing background of navigation item and adding image更改导航项的背景并添加图像
【发布时间】:2013-08-27 08:08:21
【问题描述】:

我有一个根视图控制器,我需要知道如何将其蓝色背景更改为图像或更改其颜色,并且还想在其右侧添加图像。 例如在下图中,我需要将文本根视图控制器的背景更改为另一个图像或更改其颜色并在根视图控制器文本的右侧添加一个小图标。我应该怎么做?我已经通过Change Navigation bar Background image on each navigation,但仍然无法实现它。

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

【问题讨论】:

  • 你试过了吗? [[UINavigationBar 外观]setBackgroundImage:@"navImage" forBarMetrics:UIBarMetricsDefault];
  • 假设您在 iOS 5+ 上工作?
  • 我正在使用 xcode 4.2 需要尽快升级:P

标签: iphone ios objective-c xcode navigationcontroller


【解决方案1】:

尝试将这些代码中的任何一个放入ViewWillAppear/ViewDidAppear/didFinishLaunchingWithOptions(在 appDelegate 的情况下)。

1)navigationItem.titleView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"urtitlebar.png"]];

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

3)

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"urtitlebar.png"];
[navBar setBackgroundImage:image];

编辑:(如果您使用的是旧版本的 SDK,比如 3.2.5)

创建UINavigationBar的子视图并覆盖名为-drawRect:(CGRect)rect的方法,

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

@结束

【讨论】:

  • 感谢您的回复,在您的代码的帮助下,我几乎接近解决方案,但有一些小麻烦,在您的第二个答案中是否可以从右侧加载 setFrame?在您的第三个答案中,我收到此错误“例如接收器类型 UINavigationBar 消息未声明方法 setBackgroundImage”
  • [图像 setFrame:CGRectMake(276, 0, 44, 44)];
  • 是的,我也已经调整过了 [image setFrame:CGRectMake(275, 0, 44, 44)],想知道是否可以从右侧加载它,很好。关于错误的任何想法,我是否必须导入任何其他类?
  • 是的,我收到此错误“接收器类型 UINavigationBar 例如消息未声明方法 setBackgroundImage”
  • 使用 [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];这个代替..(对于 ARC)
【解决方案2】:

尝试在 AppDelegate.m 的 didFinishLaunchingWithOptions 中使用此代码在导航栏上自定义图像。

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"yourImageName.png"] forBarMetrics:UIBarMetricsDefault];

您还可以将自定义按钮添加为条形按钮。

 // 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;

就是这样。

【讨论】:

  • 感谢您的回复,在您的第一个声明中是否可以定位图像,例如现在图像从顶部显示到 75%,但我只需要显示中心部分。对于第二部分,我知道要添加自定义按钮,我正在尝试在右侧添加 UIimage 而不是栏按钮,这可能吗?
猜你喜欢
  • 1970-01-01
  • 2016-04-27
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 2021-02-21
相关资源
最近更新 更多