盡管iOS原生的UI控件就已經(jīng)有很不錯(cuò)的顯示效果,但是App開發(fā)者仍然希望自己的產(chǎn)品與眾不同,所以自定義UI外觀成了每個(gè)App產(chǎn)品開發(fā)必做之事。今天就來做一個(gè)在iOS6下實(shí)現(xiàn)自定義UI的demo,內(nèi)容及Demo來源于國外iOS博客 raywenderlich ,先看看美化前后效果差別(左邊為美化前,右邊為美化后):
整個(gè)Demo里面幾乎包含所有iOS下的UI控件,以下我只對關(guān)鍵代碼給出說明,詳情大家可以下載附上的Demo源碼查看。好了,首先在AppDelegate.m中新建了一個(gè)方法customizeApperance(),所有的美化效果都在這個(gè)方法里完成,并且在application:didFinishLauchingWithOptions:里面調(diào)用這個(gè)方法。
1.自定義導(dǎo)航欄
//豎屏 UIImage *image44 = [[UIImage imageNamed:@"surf_gradient_textured_44"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [[UINavigationBar appearance] setBackgroundImage:image44 forBarMetrics:UIBarMetricsDefault]; //橫屏 UIImage *gradientImage32 = [[UIImage imageNamed:@"surf_gradient_textured_32"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];
其中resizableImageWithCapInsets方法是iOS5以后才有的,目的是用來指定圖片拉伸區(qū)域,其參數(shù)Insets制定可被拉伸的圖片區(qū)域,UIEdgetInsetsMake有四個(gè)參數(shù),按順序分別標(biāo)示top、left、bottom、right,指定的數(shù)字分別表示框起來的矩形框離圖片邊界的距離,只有矩形框內(nèi)的區(qū)域才會被拉伸,如下圖所示:
圖片只會被拉伸紅色矩形區(qū)域,假如紅框左右距離圖片邊距分別為25,距離上下邊距為0,則Insets的寫法就是這樣: UIEdgetInsetsMake(0,25,0,25),如果要使整張圖片拉伸,則四個(gè)參數(shù)全部傳0即可。代碼中分別指定了橫屏和豎屏下的兩種方式。
同時(shí),可以指定導(dǎo)航欄底部的陰影圖片、顏色和UIBarButtonItem的樣式等:
- 導(dǎo)航欄陰影
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"navBarShadow"]];
- 導(dǎo)航欄按鈕樣式
UIImage *button30 = [[UIImage imageNamed:@"button_textured_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)]; [[UIBarButtonItem appearance] setBackgroundImage:button30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
- 導(dǎo)航欄返回按鈕樣式
UIImage *buttonBack30 = [[UIImage imageNamed:@"button_back_textured_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)]; [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
效果如下:
2.設(shè)置底部Tab欄的樣式(背景、選中、默認(rèn))
UIImage *tabBackground = [[UIImage imageNamed:@"tab_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [[UITabBar appearance] setBackgroundImage:tabBackground]; [[UITabBar appearance] setSelectionIndicatorImage: [UIImage imageNamed:@"tab_select_indicator"]];效果如下:
3.自定義UISwitch
//指定開關(guān)兩種狀態(tài)下背景顏色和滑動(dòng)桿顏色 [[UISwitch appearance] setOnTintColor:[UIColor colorWithRed:0 green:175.0/255.0 blue:176.0/255.0 alpha:1.0]]; [[UISwitch appearance] setTintColor:[UIColor colorWithRed:1.000 green:0.989 blue:0.753 alpha:1.000]]; [[UISwitch appearance] setThumbTintColor:[UIColor yellowColor]]; //指定開關(guān)兩種狀態(tài)下背景圖片(YES、NO) [[UISwitch appearance] setOnImage:[UIImage imageNamed:@"yesSwitch"]]; [[UISwitch appearance] setOffImage:[UIImage imageNamed:@"noSwitch"]];效果如下:
4.自定義UIPageControl
//選中狀態(tài)顏色 [[UIPageControl appearance] setCurrentPageIndicatorTintColor:[UIColor colorWithRed:0 green:175.0/255.0 blue:176.0/255.0 alpha:1.0]]; //未選中狀態(tài)顏色 [[UIPageControl appearance] setPageIndicatorTintColor:[UIColor colorWithRed:0.996 green:0.788 blue:0.180 alpha:1.000]];

這里簡要說明了幾種控件的自定義效果,查看其他控件自定義效果,大家可以下載Demo仔細(xì)查看并根據(jù)自己的需要進(jìn)行定制
Demo源碼下載: 代碼
加入我們的或微信公眾賬號請查看:
Ryan's zone公眾賬號及
同時(shí)歡迎關(guān)注我的新浪微博和我交流:
@唐韌_Ryan
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
