iOS自定义右侧导航栏

4

我正在尝试为我的导航控制器中的右侧栏目设置一张图片,但是iOS 6会显示黑色发光效果。我已经尝试过一些来自stackoverflow的解决方案,但无法使其正常工作。我当前的代码如下:

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中它看起来像这样,这正是我想要的: What I want 这是在iOS6中的样子: What I currently have

2
创建按钮并将其添加到导航栏的自定义视图中。 - Dhaval Bhadania
4个回答

10

试试这个:

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;

希望这能对你有所帮助。


0

使用自定义视图:

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;

0

试试这个:

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

0
首先使用图像创建一个UIButton,然后执行以下操作:
[[UIBarbuttonItem alloc] initWithCustomView:button];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接