自定義導航控制器

Pasted Graphic.tiff

諮詢時一個ui bar button ten,設置圖片後文字沒了。

用代碼創建控制器:新建購彩大廳的控制器ILHallViewController繼承UIViewController

   UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeContactAdd]];

    

   self.navigationItem.rightBarButtonItem = barItem;

或者拖一個uibutton上去,拖一個view controller選中該button非BarButtonItem,按住controller鍵push連過去

把tabbar去掉:

Pasted Graphic 1.tiff,隱藏的系統自帶的。刪除代碼

// 移除自帶的tabBarremoveFromSuperview不會馬上銷燬,等該函數結束後才銷燬

    [self.tabBar removeFromSuperview];

修改tabBar.frame = self.tabBar.frame;

tabBar.frame = self.tabBar.bounds;

修改[self.view addSubview:tabBar];,爲

// 因爲系統自動隱藏的是系統自帶的tabBar

    [self.tabBar addSubview:tabBar];

攔截push方法,所有push過去的時候把tartar隱藏掉,自定義導航控制器,新建控制器ILNavigationController : UINavigationController,把導航控制器的class指定爲它

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    viewController.hidesBottomBarWhenPushed = YES;

    

    return [super pushViewController:viewController animated:animated];

}

所有導航條設爲紅色

獲取應用所有的導航條,在ILTabBarViewController.m

UINavigationBar *bar = [UINavigationBar appearance];//appearance是個協議

[[UIDevice currentDevice].systemVersion floatValue]>=7.0

[bar setBackgroundImage:[UIImage imageNamed:@“NavBar64”] forBarMetrics:UIBarmetricsDefault];

[bar setBackgroundImage:[UIImage imageNamed:@“NavBar”] forBarMetrics:UIBarmetricsDefault];

這樣改ios6中導航條紅色太寬了控制器從導航條下開始,ios7中把控制器的20點佔用了44+20。控制器從最上開始

抽成宏,放在pch裏,#define ios7 [[UIDevice currentDevice].systemVersion floatValue]>=7.0

設置文字白色:

NSDictionary *dict = @{

                               NSForegroundColorAttributeName : [UIColor whiteColor],

                               NSFontAttributeName : [UIFont systemFontOfSize:15//nsfontattributename  iOS6沒有,用uitextattributefont

                               };

    [bar setTitleTextAttributes:dict];

放到ILNavigationController.m的類initialize裏(第一次使用這個類或者這個類的子類的時候),爲什麼不能放在viewdidload裏:沒點擊下面的按鈕都會調用一次

if (self == [ILNavigationController class]) { // 肯定能保證只調用一次

        // 獲取應用程序中所有的導航條

        // 獲取所有導航條外觀

        UINavigationBar *bar = [UINavigationBar appearance];

        

        UIImage *navImage = nil;

        

        if (ios7) {

            navImage = [UIImage imageNamed:@"NavBar64"];

        }else{

            navImage = [UIImage imageNamed:@"NavBar"];

        }

        [bar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];

        

        

        NSDictionary *dict = @{

                               NSForegroundColorAttributeName : [UIColor whiteColor],

                               NSFontAttributeName : [UIFont systemFontOfSize:15]

                               };

        [bar setTitleTextAttributes:dict];

}



全部彩種,拖一個button

Pasted Graphic_1.tiff調節位置,麻煩

改變按鈕文字和圖片的位置,創建ILTitleButton 放在view裏。

ILTitleButton.m裏:

#import <Availability.h>

- (void)awakeFromNib

{

    self.imageView.contentMode = UIViewContentModeCenter;//文字居中,防止填充

}


// 不能使用self.titleLabel 因爲self.titleLabel內部會調用titleRectForContentRect,contentRect是按鈕的bounds

- (CGRect)titleRectForContentRect:(CGRect)contentRect

{

    CGFloat titleX = 0;

    CGFloat titleY = 0;

    NSDictionary *dict = @{

                           NSFontAttributeName : [UIFont systemFontOfSize:15]

                           };

    CGFloat titleW = 0;

    

    if (ios7) { // 判斷運行時,及當前模擬器運行在哪個系統上

         //根據文字內容,以字體15的大小計算長度

#ifdef __IPHONE_7_0 // 判斷編譯時 sdk7.0才允許編譯  不能使用self.titleLabel.text

        titleW = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine attributes:dict context:nil].size.width;

#else // sdk 6.0 編譯下面

        titleW = [self.currentTitle sizeWithFont:[UIFont systemFontOfSize:15]].width;

        

#endif

    }else{

        titleW = [self.currentTitle sizeWithFont:[UIFont systemFontOfSize:15]].width;

    }

    

    

    CGFloat titleH = contentRect.size.height;

    

    return CGRectMake(titleX, titleY, titleW, titleH);

}


- (CGRect)imageRectForContentRect:(CGRect)contentRect

{

    CGFloat imageW = 30;

    CGFloat imageH = contentRect.size.height;

    CGFloat imageX = contentRect.size.width - imageW;

    CGFloat imageY = 0;

    

    return CGRectMake(imageX, imageY, imageW, imageH);

}




ps:

六種定義button類型: 

     UIButtonTypeCustom = 0,   無類型

     UIButtonTypeRoundedRect,   四個角是圓弧  型的   1337602973_5632.png


     UIButtonTypeDetailDisclosure    1337602937_7980.png

     UIButtonTypeInfoLight    1337602948_2706.png


     UIButtonTypeInfoDark    1337602958_9447.png


     UIButtonTypeContactAdd    1337602966_5448.png







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