2017-07-14 98 views
0

我有一個導航欄按鈕。我使用它的形象,但我面臨的問題是,如果我點擊按鈕它改變它的位置,並從導航欄下移到我的視圖控制器的視圖。我的代碼,導航欄項目調整在IOS

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
           initWithTitle:@"" 
           style:UIBarButtonItemStyleDone 
           target:self 
           action:@selector(rightBtnClicked:)]; 
[rightButton setImage:[UIImage imageNamed:@"right.png"]]; 
rightButton.tintColor=[UIColor whiteColor]; 

self.navigationItem.rightBarButtonItem = rightButton; 

enter image description here

enter image description here

+0

沒有你試過這種https://stackoverflow.com/a/5419719/4955401? –

+0

您提供的鏈接不顯示導航欄中的按鈕。 @ParvBhasker –

+0

'rightBtnClicked'發生了什麼? – Codus

回答

1

我只是檢查這是工作

UIButton *someButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; 
[someButton setImage:[UIImage imageNamed:@"IMG_1148.jpg"] forState:UIControlStateNormal]; 
[someButton addTarget:self action:@selector(requestButton) 
    forControlEvents:UIControlEventTouchUpInside]; 


UIBarButtonItem *barButton =[[UIBarButtonItem alloc] initWithCustomView:someButton]; 
self.navigationItem.rightBarButtonItem=barButton; 

Ø你可以這樣做

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0, 0, 20, 20); 
UIImage *image = [[UIImage imageNamed:@"back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
[button setImage:image forState:UIControlStateNormal]; 
button.tintColor = [UIColor redColor]; 
[button addTarget:self action:@selector(requestButton) 
    forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:button]; 
self.navigationItem.rightBarButtonItem=mailbutton; 
+0

@umer - 你有沒有時間檢查這個,因爲我只是檢查它在xcode 8.3.3中工作。 –

+0

我想要這個白色。 @Parv Bhasker –

+0

我更新了色調的代碼。 –

0

試試這個代碼:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@「right.png」] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClicked:)]; 
    self.navigationController.navigationItem.rightBarButtonItem = barButton; 
+0

它沒有顯示我的形象,因爲我跟着我們的代碼。 @Brijesh Shiroya –

0

嘗試這樣,我希望它會幫助你。

// OBJC

UIImage *img_table =[UIImage imageNamed:@"right.png"]; 

UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom]; 
button.frame = CGRectMake(0, 0, img_table.size.width, img_table.size.height); 
[button setImage:img_table forState: UIControlStateNormal]; 
[button setImage:img_table forState: UIControlStateSelected]; 
[button addTarget: self action:@selector(rightBtnClicked:) forControlEvents: UIControlEventTouchUpInside]; 

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 
barButton.tintColor=[UIColor whiteColor]; 
self.navigationItem.rightBarButtonItem = barButton; 

//斯威夫特

let img_table = UIImage(named: "right.png") 
    let button = UIButton.init(type: UIButtonType.custom) 
    button.frame = CGRect.init(x: 0, y: 0, width: (img_table?.size.width)!, height: (img_table?.size.height)!) 
    button.setImage(img_table, for: UIControlState.normal) 
    button.setImage(img_table, for: UIControlState.selected) 

    button.addTarget(self, action: @selector(rightBtnClicked:)), for: UIControlEvents.touchUpInside) 
    let barButton = UIBarButtonItem.init(customView: button) 
    barButton.tintColor=[UIColor whiteColor]; 
    self.navigationItem.rightBarButtonItem = barButton 
0

的問題是,你需要提供一個幀圖像。因此,採取一個UIButton並設置圖像和行動。然後將UIButton添加到UIBarButtonItem。

下面是工作代碼爲你:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(rightButtonClicked:)forControlEvents:UIControlEventTouchUpInside]; 
[button setFrame:CGRectMake(0, 0, 53, 31)]; 
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 
self.navigationItem.rightBarButtonItem = barButton; 
+0

非常感謝您的信息:)。 @Nirmalsinh –