1. 程式人生 > >IOS 自定義導航欄和標籤欄

IOS 自定義導航欄和標籤欄

大多數ios應用都是以標籤欄加導航欄的形式呈現,一般根控制器都是UIToolbar,然後再以UINavigationController為子控制器,然後再加入UIViewController。為什麼要這樣?因為不同的標籤欄的項一般對應不同的功能,那麼導航欄的標題一般就不同。如果所有導航欄都一樣,那你就隨便設定根控制器。
主要程式碼:AppDelegate.h中
@class cscControllerOne;
@class cscControllerTwo;
@class cscControllerThree;
@class cscControllerFour;
@interface cscAppDelegate : UIResponder {

 cscControllerOne *oneRootView;
 cscControllerTwo *twoRootView;
 cscControllerThree *threeRootView;
 cscControllerFour  *fourRootView;
 UITabBarController *tabBarController;
 UINavigationController *unc1;
 UINavigationController *unc2;
 UINavigationController *unc3;
 UINavigationController *unc4;
}

AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
 // Override point for customization after application launch.
 oneRootView=[[cscControllerOne alloc]init];

 [email protected]"檢視1";
 twoRootView=[[cscControllerTwo alloc]init];
 [email protected]"檢視2";
 threeRootView=[[cscControllerThree alloc]init];
 [email protected]"檢視3";
 fourRootView=[[cscControllerFour alloc]init];
 [email protected]"檢視4";
 unc1=[[UINavigationController alloc]initWithRootViewController:oneRootView];
 unc2=[[UINavigationController alloc]initWithRootViewController:twoRootView];
 unc3=[[UINavigationController alloc]initWithRootViewController:threeRootView];
 unc4=[[UINavigationController alloc]initWithRootViewController:fourRootView];
 unc1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"淘寶搜尋" image:[UIImage imageNamed:@"Area.png"]  tag:2];
 unc2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的淘寶" image:[UIImage imageNamed:@"Summary.png"]  tag:3];
 unc3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"淘寶功能" image:[UIImage imageNamed:@"Apple.png"]  tag:4];
 unc4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"軟體設定" image:[UIImage imageNamed:@"BoxVolume.png"]  tag:5];
 NSArray *viewsArray=[[NSArray alloc]initWithObjects:unc1,unc2,unc3,unc4, nil];
 tabBarController=[[UITabBarController alloc]init];
 tabBarController.viewControllers=viewsArray;

 self.window.rootViewController = tabBarController;
 [self.window makeKeyAndVisible];
 return YES;
}
可能這段程式碼並不難,剛入門的新手都能輕鬆做出來,但是一個新手與一個有多年開發經驗的高手,區別之一在於程式碼的積累,所以大量的案例,是一個高手的必備,是二次開發的利器。
下載地址http://www.kuaipan.cn/file/id_30491149655343756.htm
有時候可能會遇到在push之後隱藏標籤欄,其實很簡單,只要一條語句:
self.hidesBottomBarWhenPushed =NO;
下載一個push隱藏的例子
效果圖:
IOS開發隨意定製標籤欄任意設計自己特色的導航欄總結IOS開發隨意定製標籤欄任意設計自己特色的導航欄總結

IOS開發隨意定製標籤欄任意設計自己特色的導航欄總結IOS開發隨意定製標籤欄任意設計自己特色的導航欄總結IOS開發隨意定製標籤欄任意設計自己特色的導航欄總結


效果圖二:圖中第二到第五張圖是iphone5真機中的效果,自定義標籤欄,萬能的方法。
有自定義的標籤欄,自定義的導航欄,隱藏標籤欄,隱藏導航欄,並且不阻塞viewWillAppear,自定義導航欄的返回按鈕
具體辦法是隱藏系統自帶的標籤欄,繪製自己的標籤欄,然後為每個標籤設定點選事件。

 self.customTabBar = [[[CustomTabBar alloc] init] autorelease];
 self.customTabBar.tabBarController = self.viewController;
 [self.customTabBar custom];
 [self.viewController.tabBarController.tabBar setHidden:YES];
自定義一個按鈕,然後加到導航欄中,可以是替換系統的返回按鈕
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
 btn.frame=CGRectMake(0, 0, 60, 30);
 [btn setBackgroundImage:[UIImage imageNamed:@"按鈕p.png"] forState:UIControlStateNormal];
//  [btn setTitle:@"aaaaaa" forState:UIControlStateNormal];
 [btn setBackgroundImage:[UIImage imageNamed:@"按鈕n.png"] forState:UIControlStateHighlighted];  // highlighted
 [btn addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
 UIBarButtonItem *BarBtn = [[UIBarButtonItem alloc] initWithCustomView:btn ];
 self.navigationItem.leftBarButtonItem=BarBtn;
 [BarBtn release];
 [btn release];
設定系統樣式UIBarButtonItem按鈕
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStyleBordered target:self action:@selector(Action)] autorelease];
 self.navigationItem.rightBarButtonItem = addButton;
設定一個包含自定義圖示的UIBarButtonItem按鈕
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"email.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(action:)];
self.navigationItem.rightBarButtonItem = addButton;
設定一個分段控制元件替換導航欄上的按鈕
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects: [UIImage imageNamed:@"up.png"], [UIImage imageNamed:@"down.png"], nil]];
 [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
 segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
 segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
 segmentedControl.momentary = YES;
 UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
 [segmentedControl release];
 self.navigationItem.rightBarButtonItem = segmentBarItem;
 [segmentBarItem release];
中間第二個標籤有一個遠較,是因為那部分圖片是透明的,根據需要可以設定標籤欄的樣式。
點選下載http://www.kuaipan.cn/file/id_30491149655345680.htm
設定導航欄為半透明
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
設定導航欄為透明
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
設定導航欄為預設
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
設定一個分段控制元件替換標題檢視
NSArray *segmentTextContent = [NSArray arrayWithObjects: NSLocalizedString(@"Image", @""), NSLocalizedString(@"Text", @""), NSLocalizedString(@"Video", @""), nil];
 UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
 segmentedControl.selectedSegmentIndex = 0;
 segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
 segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
 segmentedControl.frame = CGRectMake(0, 0, 400, kCustomButtonHeight);
 [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
 self.navigationItem.titleView = segmentedControl;
 [segmentedControl release];
判斷選中事件
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
 NSLog(@"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
在導航欄上顯示一行字
self.navigationItem.prompt = NSLocalizedString(@"Please select the appropriate media type:", @"Page Five Prompt"); IOS開發隨意定製標籤欄任意設計自己特色的導航欄總結          IOS開發隨意定製標籤欄任意設計自己特色的導航欄總結 彈出式選單欄的做法,其實就是自定義view;然後以某一個檢視為根檢視,進行其它操作。首先是uiview,uiview中加入scrollview,一般情況下是scrollview無法滑動,但是加入一個方法之後就可以了

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

 //所點選的位置在view的範圍

    if (point.y < 0) {

        [self showFullAppBar];

        [_selectedButton setSelected:NO];

        return NO;

    }

 return YES;

相關推薦

IOS 定義導航標籤

大多數ios應用都是以標籤欄加導航欄的形式呈現,一般根控制器都是UIToolbar,然後再以UINavigationController為子控制器,然後再加入UIViewController。為什麼要這樣?因為不同的標籤欄的項一般對應不同的功能,那麼導航欄的標題一般就不同。

iOS -- 導航標籤的一些設定

UINavigationBar && UITabbarItem相關 設定導航欄字型的大小以及顏色 [[UINavigationBar appearance] setTit

iOS-定義導航後側滑返回功能失效

2016-03-17 iOS開發 文/煜寒了(簡書作者) 原文連結:http://www.jianshu.com/p/349636eb3fca iPhone有一個回退按鈕在所有的導航條上.這是一個簡單的沒有文字箭頭. 在一開始寫專案的時候,就要做好一個準備,導航欄是自定義還

iOS開發】---- 表格滾動時隱藏及顯示導航標籤

在iOS開發中,以瀑布流瀏覽圖片時通常希望能更多空間來展示內容,這樣我們就希望UIScrollView滾動時隱藏及顯示導航條和標籤欄。 我們希望向下滾動時顯示,向上滾動時隱藏,同時希望隱藏和顯示的動畫

[iOS 定義導航/導航條]隱藏系統導航

先看效果 第一種 程式碼裡改變了導航條顏色 第二種 固定導航顏色的 其實這兩個沒什麼區別,主要是封裝了一個導航條NavigationBar 繼承自UIView 思路 使用程式碼,例項化導航條(前提是已經在設定導航控制器的時候 隱

iOS 定義導航pop返回手勢,全屏右滑返回

- (void)viewDidLoad {     [superviewDidLoad]; //重新給interactivePopGestureRecognizer.delegate掛代理

iOS定義導航,側滑返回卡死情況的解決辦法

側滑返回是iOS7新增的一個特性,如果自定義了導航欄,那麼側滑返回這個功能就會失效。雖然在自定義導航欄的viewDidLoad方法中新增self.interactivePopGestureRecognizer.delegate = (id)self;一行程式碼就

java struts定義型別轉換標籤的使用

今天和大家分享一下struts(自定義轉換型別以及標籤的使用) 自定義型別轉換需要兩個步驟,首先需定義相應的型別轉換器,然後想struts框架註冊型別轉換器。(分享一個字串與Line型別之間的轉換) (1).struts框架提供了轉換器類定義的方法。 繼承Default

ios開發ios7定義導航模擬器真機執行介面座標不一致

做了一個簡單的自定義導航欄,也就正常的設定了返回按鈕圖片和導航背景圖片,結果在真機上執行的時候突然發現介面的空間向下移了,列印了一下y座標,移動了64,又列印了一下self.view的y座標,盡然也向下移動了64,但是

IOS初學-定義導航中的內容

對導航欄中進行簡單的自定義操作 新建一個FourSubViewController檢視控制器 self.title="FourPage"         self.view.backgroundColor=UIColor.brown &nb

iOS開發之定義導航返回按鈕右滑返回手勢失效的解決---親測是有效的。

問題一:怎麼自定義leftItem問題二:為什麼系統自帶的右滑返回手勢失效問題三:怎麼解決這個失效問題3.怎麼解決這個失效問題 其實很簡單很簡單~只需要新增下面這一句程式碼即可self.navigationController.interactivePopGestureRe

android 定義狀態列導航分析與實現

效果   android 4.4之後,系統是支援自定義狀態列和導航欄的,舉個最典型的例子就是bilibili客戶端了(iOS版本和android版本能用兩套完全不一樣符合各自系統的設計ui,良心啊~),頂部狀態列為粉色,底部導航欄為半透明色:      接

iOS開發之定義導航返回按鈕右滑返回手勢失效的解決

我相信針對每一個iOS開發者來說~除了根檢視控制器外~所有的介面通過導航欄push過去的介面都是可以通過右滑來返回上一個介面~其實~在很多應用和APP中~使用者已經習慣了這個功能~然而~作為開發者的我們~也並沒有為此做些什麼~因為我們在建立專案時~蘋果公司已經為我們都做好了~那麼~我們這期就來聊一聊這系統

Swift- 定義導航背景、標題返回按鈕屬性

廢話少說,直接 看程式碼: //navigationBar的預設背景顏色 // UINavigationBar.appearance().tintColor = UIColor

IOS 開發筆記——定義導航返回鍵後如何恢復系統預設返回手勢

       在開發當中,我們不得不要將導航欄的左邊返回按鈕自定義成不同的樣式,但是一當我們自定義樣式後,系統預設的 pop 手勢就失效了,那麼我們該如何做呢?其實這個問題網上也有很多答案,但是每一個答案都非常坑,我都不得不吐槽一下了,老是將問題複雜化,不懂裝懂,最討厭這些

iOS 獲取導航狀態的高度

bsp statusbar pan frame bar gin cgrect tom uia CGRect rect = [[UIApplication sharedApplication] statusBarFrame]; 狀態欄的高度: float status

微信小程式開發筆記2——定義導航元件

本文主要是熟悉微信小程式自定義元件的開發,以一個常見的導航欄(Tabbar)需求為例。 官方文件:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/ 首先我們先看一

微信小程式定義導航 navigation bar 返回鍵 首頁

微信小程式自定義導航欄(wx_custom_navigation_bar) 自定義返回鍵、首頁鍵,動態設定標題,響應式元件 版本號:1.0.0 作者:chen-yt github: https://github.com/chen-yt/wx_custom_navigation

小程式定義導航

將app.json裡的navigationStyle設定為navigationStyle:custom。可以在.wxss裡通過margin和padding來自定義導航欄。 例如在.wxml裡寫 <view class="nav">我是汽車維修技師</view>

CSS實現定義導航帶二級選單

程式碼如下: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> &