10
A
回答
19
希望這有助於
viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];
viewController.navigationItem.rightBarButtonItem = item;
1
UIView *viewWithButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
//view customization
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:leftButton];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:rightButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:viewWithButtons];
[viewWithButtons release];
8
這裏的代碼,只需撥打下面的方法從viewDidLoad
方法
- (void)addCustomButtonOnNavBar
{
UIBarButtonItem * item1= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"passImageNmae1"] style:UIBarButtonItemStylePlain target:self action:@selector(yourButtonAction1)];
UIBarButtonItem * item2= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"passImageNmae2"] style:UIBarButtonItemStylePlain target:self action:@selector(yourButtonAction2)];
NSArray * buttonArray =[NSArray arrayWithObjects:item1,item2 ,nil];
self.navigationItem.rightBarButtonItems =buttonArray;
}
+1
這是行不通的。它試圖把圖像放在一個按鈕上。我希望圖像本身就是按鈕。 –
+0
換句話說,我無法實現使用此代碼發佈的屏幕截圖。 –
+0
@Karamshad Button非常大。如何設置適合導航欄的正確大小? –
1
我用一個類別是:
的UIBarButtonItem + WithImageOnly。 h:
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage*)image target:(id)target action:(SEL)action;
@end
的UIBarButtonItem + WithImageOnly.m:
#import "UIBarButtonItem+WithImageOnly.h"
@implementation UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage *)image target:(id)target action:(SEL)action {
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
frame = CGRectInset(frame, -5, 0);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [self initWithCustomView:button];
}
@end
用法:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImageOnly:[UIImage imageNamed:@"image"] target:self action:@selector(someAction:)]
相關問題
- 1. IOS半透明導航欄,但堅實的導航欄按鈕
- 2. 帶有多個按鈕的導航欄
- 3. iOS:在自定義導航欄中定位導航欄按鈕
- 4. 導航欄按鈕
- 5. 添加按鈕導航欄ios
- 6. IOS 11導航欄按鈕項目
- 7. iOS自定義導航欄按鈕項
- 8. 帶圖像的水平導航欄
- 9. ios導航按鈕
- 10. 導航帶後退按鈕的左欄按鈕項目
- 11. 導航欄圖片隱藏自定義導航欄按鈕
- 12. 系統圖標和圖像的自定義導航欄按鈕
- 13. 帶導航按鈕的QLPreviewController
- 14. ios導航欄項目圖像大小
- 15. 按下後退按鈕後導航欄圖像保留
- 16. 導航欄和按鈕
- 17. 更改導航欄按鈕
- 18. jQuery和導航欄按鈕
- 19. 旋轉導航欄按鈕
- 20. iphone 2按鈕導航欄
- 21. 帶有「後退」和「保存」按鈕的iOS導航視圖?
- 22. iOS中的導航欄按鈕圖像需要15秒才能加載
- 23. 如何設置圖像默認後退導航欄的按鈕
- 24. 導航欄上的圖像後退按鈕
- 25. 圖像和文本的導航欄按鈕項目。 swift
- 26. 更改導航欄按鈕的背景圖像
- 27. 如何在導航欄的按鈕上設置圖像?
- 28. 標題和按鈕之間的導航欄圖像
- 29. 更新整個項目的導航欄後退按鈕圖像
- 30. 如何更改導航欄上後退按鈕的圖像?
我想這就是我要找的。我該如何處理這個按鈕的觸摸? –
不錯!我試過這個和下面的Kamarshad答案,但這個答案好多了,因爲按鈕圖標看起來不錯,「initWithCustomView」 – swiftBoy