IOS系列——導航條的多種操作

1)導航控制器的創建及導航條顏色、背景圖片的設置:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
//    self.window.rootViewController = self.viewController;


    //創建導航控制器
    UINavigationController *nvc=[[[UINavigationController alloc]initWithRootViewController:vc]autorelease];
    self.window.rootViewController=nvc;
    
    
    //修改顏色
//    nvc.navigationBar.tintColor=[UIColor blueColor];
    //修改NAvigationBar的背景圖片
    //獲取背景圖片
    NSString *str=[[NSBundle mainBundle]pathForResource:@"navigationbar_background" ofType:@"png"];
    UIImage *barImg=[[UIImage alloc]initWithContentsOfFile:str];
    //設置背景圖片
    [nvc.navigationBar setBackgroundImage:barImg forBarMetrics:UIBarMetricsDefault];
    

    [self.window makeKeyAndVisible];
    return YES;
}


2)導航條添加一個UITextField

//如果存在導航控制器,則設置導航條
    if (self.navigationController)
    {
        //創建一個UITextField
        UITextField *txtURL=[[[UITextField alloc]initWithFrame:CGRectMake(10, 5, 200, 25)]autorelease];
        //將txt改爲圓角
        txtURL.borderStyle=UITextBorderStyleRoundedRect;
        //設置字體大小
        txtURL.font=[UIFont systemFontOfSize:12];
        //設置提示
        txtURL.placeholder=@"請輸入網址";
        //設置鍵盤的return類型爲GO
        txtURL.returnKeyType=UIReturnKeyGo;
        //設置代理
        txtURL.delegate=self;
        self.txtURL=txtURL;
        
        
        //創建UIBarButtonItem,添加self.txtURL
        UIBarButtonItem *txtURLBtn=[[[UIBarButtonItem alloc]initWithCustomView:self.txtURL]autorelease];
        //設置導航條的左側按鈕
        self.navigationItem.leftBarButtonItem=txtURLBtn;
    }

3)導航條添加一個UIButton
UIButton *cancelBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        //設置按鈕的位置
        cancelBtn.frame=CGRectMake(200, 5, 80, 25);
        //設置按鈕的title
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        UIBarButtonItem *rightBtn=[[[UIBarButtonItem alloc]initWithCustomView:cancelBtn]autorelease];
        self.navigationItem.rightBarButtonItem=rightBtn;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchView:)];

 無論是設置self.navigationItem.leftBarButtonItem.title =@"返回";還是self.navigationItem.backBarButtonItem.title =@"返回";都沒有效果,title文本始終不會發生變化。到網上亂搜一通後,得到了以下解決方法,相對來說比較簡單

    在第一級頁面的viewDidLoad方法中加入以下代碼:

UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title = @"返回";
    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    [temporaryBarButtonItem release];

    

    也就是用一個新的按鈕在進行導航前將原來的返回按鈕替換掉就可以了。



要求導航欄的返回按鈕只保留那個箭頭,去掉後邊的文字,在網上查了一些資料,最簡單且沒有副作用的方法就是
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
 forBarMetrics:UIBarMetricsDefault];


隱藏導航條返回按鈕

方法1、在上一級界面即將跳轉的時候

UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title = @"";
     self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
     [temporaryBarButtonItem release]; 

方法2、

self.navigationItem.hidesBackButton = YES



設置導航條顏色要和背景色 左右按鈕的顏色

self.navigationController.navigationBar.barTintColor = MAINCOLOR;			//導航條顏色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];		//導航條按鈕顏色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                               [UIColor whiteColor], NSForegroundColorAttributeName, nil]];  //導航條title文字顏色

如果有多個按鈕,設置某一個按鈕顏色可以使用
self.itemSave.tintColor = [UIColor blackColor];



更多運用 在: http://blog.csdn.net/hufengvip/article/details/17193359

發佈了59 篇原創文章 · 獲贊 3 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章