繁体   English   中英

iOS自定义右侧导航栏

[英]iOS custom right navigation bar

我正在尝试在导航控制器中为右边的条形项设置图像,但iOS 6会一直显示黑色光晕。 我已尝试过堆栈溢出的许多解决方案但无法使其工作。 我目前的代码是这样的:

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gear"]
                                                              style:UIBarButtonItemStylePlain
                                                             target:self
                                                             action:@selector(someMethod)];
[rightItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];

[[self navigationItem] setRightBarButtonItem:rightItem];

在iOS7中看起来像这是我想要的: 我想要的是 这就是它在iOS6中的外观 我现在有什么

试试这个:

UIImage *faceImage = [UIImage imageNamed:@"gear.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );//set bound as per you want
[face addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.rightBarButtonItem = backButton;

它可以帮到你。

首先使用图像创建UIButton,然后执行:

[[UIBarbuttonItem alloc] initWithCustomView:button];

使用自定义视图:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; // change this to use your image
[rightButton addTarget:self
                    action:@selector(yourAction:)
          forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightButtonItem;

尝试这个:

UIImage* image = [UIImage imageNamed:@"sample_Image.png"];

CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(MethodName:)
     forControlEvents:UIControlEventTouchUpInside];   

UIBarButtonItem *barBtn =[[UIBarButtonItem alloc] initWithCustomView:someButton];

    [self.navigationItem setRightBarButtonItem:barBtn];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM