轉載自http://my.oschina.net/plumsoft/blog/55927
前面的一篇文章《iOS開發16:使用Navigation Controller切換視圖》中的小例子在運行時,屏幕上方出現的工具欄就是Navigation Bar,而所謂UINavigationItem就可以理解為Navigation Bar中的內容,通過編輯UINavigationItem,我們可以使得在Navigation Bar中顯示想要的東西,比如設置標題、添加按鈕等。
這篇博客將會以一個小例子來演示如何設置UINavigationItem。
現在我用的是Xcode 4.3,在使用上跟Xcode 4.2差不多。
1、首先運行Xcode 4.3,創建一個Single View Application,名稱為UINavigationItem Test:
2、其次,我們要使得程序運行時能夠顯示Navigation Bar:
2.1 單擊AppDelegate.h,向其中添加屬性:
@property (strong, nonatomic) UINavigationController *navController;
2.2 打開AppDelegate.m,在@synthesize viewController = _viewController;之后添加代碼:
@synthesize navController; #pragma mark - #pragma mark Application lifecycle
2.3 修改didFinishLaunchingWithOptions方法代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; [self.window addSubview:navController.view]; [self.window makeKeyAndVisible]; return YES; }
此時運行程序,會發現出現了Navigation Bar:
下面講一下關於NavigationItem的簡單設置。
3、設置標題:
打開ViewController.m,在viewDidLoad方法中[super viewDidLoad];之后添加代碼:
self.navigationItem.title = @"標題";
運行:
4、自定義標題,設置titleView:
如果我們想改變標題的顏色和字體,就需要自己定義一個UILabel,並且已經設置好這個Label的內容,可以設置自己想要的字體、大小和顏色等。然后執行self.navigationItem.titleView = myLabel;就可以看到想要的效果。
4.1 打開ViewController.h,向其中添加屬性:
@property (strong, nonatomic) UILabel *titleLabel;
4.2 打開ViewController.m,在@implementation ViewController下面一行添加代碼:
@synthesize titleLabel;
4.3 在viewDidLoad方法中,去掉self.navigationItem.title = @"標題";,並添加代碼:
//自定義標題 titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)]; titleLabel.backgroundColor = [UIColor clearColor]; //設置Label背景透明 titleLabel.font = [UIFont boldSystemFontOfSize:20]; //設置文本字體與大小 titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1]; //設置文本顏色 titleLabel.textAlignment = UITextAlignmentCenter; titleLabel.text = @"自定義標題"; //設置標題 self.navigationItem.titleView = self.titleLabel;
運行:
實際上,不僅僅可以將titleView設置成Label,只要是UIView的對象都可以設為titleView,例如,將4.3中的代碼改成:
UIButton *button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect]; [button setTitle: @"按鈕" forState: UIControlStateNormal]; [button sizeToFit]; self.navigationItem.titleView = button;
則運行起來效果如下:
5、為Navigation Bar添加左按鈕
以下是進行leftBarButtonItem設置的代碼:
self.navigationItem.leftBarButtonItem = (UIBarButtonItem *) self.navigationItem.leftBarButtonItems = (UIBarButtonItem *) self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *) self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *) animated:(BOOL) self.navigationItemsetLeftBarButtonItems:(NSArray *) self.navigationItemsetLeftBarButtonItems:(NSArray *) animated:(BOOL)
其實很簡單,只要定義好一個UIBarButtonItem,然后執行上述某行代碼就行了。
5.1 為了使得運行時不出錯,我們在ViewController.m中添加一個空方法,由將要創建的左右按鈕使用:
//空方法 -(void)myAction { }
5.2 添加一個左按鈕:
在ViewDidLoad方法最后添加代碼:
//添加左按鈕 UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左按鈕" style:UIBarButtonItemStylePlain target:self action:@selector(myAction)]; [self.navigationItem setLeftBarButtonItem:leftButton];
運行效果如下:
創建一個UIBarButtonItem用的方法主要有:
[UIBarButtonItemalloc]initWithTitle:(NSString *) style:(UIBarButtonItemStyle) target:(id) action:(SEL) [UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)
在第一個方法中,我們可以使用的按鈕樣式有:
UIBarButtonItemStyleBordered UIBarButtonItemStyleDone UIBarButtonItemStylePlain
效果分別如下:
看上去第一個和第三個樣式效果是一樣的。
6、添加一個右按鈕
在ViewDidLoad方法最后添加代碼:
//添加右按鈕 UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(myAction)]; self.navigationItem.rightBarButtonItem = rightButton;
運行如下:
這里創建UIBarButtonItem用的方法是
[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)
用了系統自帶的按鈕樣式,這些樣式的標簽和效果如下:
標簽 | 效果 | 標簽 | 效果 |
UIBarButtonSystemItemAction | ![]() |
UIBarButtonSystemItemPause | ![]() |
UIBarButtonSystemItemAdd | ![]() |
UIBarButtonSystemItemPlay | ![]() |
UIBarButtonSystemItemBookmarks | ![]() |
UIBarButtonSystemItemRedo | ![]() |
UIBarButtonSystemItemCamera | ![]() |
UIBarButtonSystemItemRefresh | ![]() |
UIBarButtonSystemItemCancel | ![]() |
UIBarButtonSystemItemReply | ![]() |
UIBarButtonSystemItemCompose | ![]() |
UIBarButtonSystemItemRewind | ![]() |
UIBarButtonSystemItemDone | ![]() |
UIBarButtonSystemItemSave | ![]() |
UIBarButtonSystemItemEdit | ![]() |
UIBarButtonSystemItemSearch | ![]() |
UIBarButtonSystemItemFastForward | ![]() |
UIBarButtonSystemItemStop | ![]() |
UIBarButtonSystemItemOrganize | ![]() |
UIBarButtonSystemItemTrash | ![]() |
UIBarButtonSystemItemPageCurl | ![]() |
UIBarButtonSystemItemUndo | ![]() |
注意,UIBarButtonSystemItemPageCurl只能在Tool Bar上顯示。
7、添加多個右按鈕
在ViewDidLoad方法中最后添加代碼:
//添加多個右按鈕 UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(myAction)]; UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; UIBarButtonItem *rightButton3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(myAction)]; UIBarButtonItem *rightButton4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *rightButton5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(myAction)]; NSArray *buttonArray = [[NSArray alloc] initWithObjects:rightButton1,rightButton2, rightButton3,rightButton4,rightButton5, nil]; self.navigationItem.rightBarButtonItems = buttonArray;
為了更好的顯示效果,把設置titleView以及設置leftBarButtonItem的代碼注釋掉,運行效果如下:
上面的UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系統提供的用於占位的按鈕樣式。
8、設置Navigation Bar背景顏色
在viewDidLoad方法后面添加代碼:
//設置Navigation Bar顏色 self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(218.0/255.0) green:(228.0 / 255.0) blue:(250.0 / 255.0) alpha:1];
運行如下:
9、設置Navigation Bar背景圖片
首先將准備好作為背景的圖片拖到工程中,我用的圖片名稱是title_bg.png。
將上面的代碼改成:
//設置Navigation Bar背景圖片 UIImage *title_bg = [UIImage imageNamed:@"title_bg.png"]; //獲取圖片 CGSize titleSize = self.navigationController.navigationBar.bounds.size; //獲取Navigation Bar的位置和大小 title_bg = [self scaleToSize:title_bg size:titleSize];//設置圖片的大小與Navigation Bar相同 [self.navigationController.navigationBar setBackgroundImage:title_bg forBarMetrics:UIBarMetricsDefault]; //設置背景
之后,在ViewController.m中添加一個方法用於調整圖片大小:
//調整圖片大小 - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ UIGraphicsBeginImageContext(size); [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
運行: