簡體   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