基于UI控件的實(shí)現(xiàn)原理蹄皱,開(kāi)發(fā)者完全可以開(kāi)發(fā)出項(xiàng)目定制的控件——當(dāng)iOS系統(tǒng)提供的UI控件不足以滿(mǎn)足項(xiàng)目需要時(shí)览闰,開(kāi)發(fā)者可以通過(guò)繼承UIView來(lái)派生自定義控件芯肤。
#當(dāng)開(kāi)發(fā)者打算派生自己的UI控件時(shí),首先定義一個(gè)繼承View基類(lèi)的子類(lèi)压鉴,然后重寫(xiě)View類(lèi)的一個(gè)或多個(gè)方法崖咨,通常可以被用戶(hù)重寫(xiě)的方法如下
? initWithFrame::前面已經(jīng)見(jiàn)到油吭,程序創(chuàng)建UI控件時(shí)常常會(huì)調(diào)用該方法執(zhí)行初始化击蹲,因此,如果你需要對(duì)UI控件執(zhí)行一些額外的初始化婉宰,即可通過(guò)重寫(xiě)該方法來(lái)實(shí)現(xiàn)歌豺。
? initWithCoder::程序通過(guò)在nib文件中加載完該控件后會(huì)自動(dòng)調(diào)用該方法。因此心包,如果程序需要在nib文件中加載該控件后執(zhí)行自定義初始化类咧,則可通過(guò)重寫(xiě)該方法來(lái)實(shí)現(xiàn)。
? drawRect::如果程序需要自行繪制該控件的內(nèi)容蟹腾,則可通過(guò)重寫(xiě)該方法來(lái)實(shí)現(xiàn)痕惋。
? layoutSubviews:如果程序需要對(duì)該控件所包含的子控件布局進(jìn)行更精確的控制,可通過(guò)重寫(xiě)該方法來(lái)實(shí)現(xiàn)娃殖。
? didAddSubview::當(dāng)該控件添加子控件完成時(shí)值戳,將會(huì)激發(fā)該方法。
? willRemoveSubview::當(dāng)該控件將要?jiǎng)h除子控件時(shí)珊随,將會(huì)激發(fā)該方法述寡。
? willMoveToSuperview::當(dāng)該控件將要添加到其父控件中時(shí),將會(huì)激發(fā)該方法叶洞。
? didMoveToSuperview:當(dāng)把該控件添加到父控件完成時(shí)鲫凶,將會(huì)激發(fā)該方法。
? willMoveToWindow: :當(dāng)該控件將要添加到窗口中時(shí)衩辟,將會(huì)激發(fā)該方法螟炫。
? didMoveToWindow:當(dāng)把該控件添加到窗口完成時(shí),將會(huì)激發(fā)該方法艺晴。
? touchesBegan:withEvent::當(dāng)用戶(hù)手指開(kāi)始觸碰該控件時(shí)昼钻,將會(huì)激發(fā)該方法。
? touchesMoved:withEvent::當(dāng)用戶(hù)手指在該控件上移動(dòng)時(shí)封寞,將會(huì)激發(fā)該方法然评。
? touchesEnded:withEvent::當(dāng)用戶(hù)手指結(jié)束觸碰該控件時(shí),將會(huì)激發(fā)該方法狈究。
? touchesCancelled:withEvent::用戶(hù)取消觸碰該控件時(shí)碗淌,將會(huì)激發(fā)該方法。
#靈活運(yùn)用代理/block進(jìn)行值處理,等等亿眠。碎罚。。
#自定義Navigation
1.更改導(dǎo)航欄的背景和文字Color
#方法1:
//set NavigationBar 背景顏色&title 顏色
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
#方法2:
//設(shè)置NavigationBar背景顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
//@{}代表Dictionary
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
在導(dǎo)航欄使用背景圖片:
如果您的應(yīng)用程序使用了自定義圖像作為欄的背景纳像,你需要提供一個(gè)“更大”的圖片荆烈,使其延伸了狀態(tài)欄的后面。
導(dǎo)航欄的高度現(xiàn)在是從44點(diǎn)(88像素)更改為64點(diǎn)(128像素)竟趾。仍然可以使用了setBackgroundImage:方法來(lái)指定自定義圖像的導(dǎo)航欄憔购。
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
#改變導(dǎo)航欄標(biāo)題的字體
#可以通過(guò)使用導(dǎo)航欄的“titleTextAttributes”屬性來(lái)自定義的文本樣式。
#可以指定字體潭兽,文字顏色倦始,文字陰影顏色,文字陰影在文本標(biāo)題偏移屬性字典山卦,使用下面的文本屬性鍵:
#UITextAttributeFont - 字體
#UITextAttributeTextColor - 文字顏色
#UITextAttributeTextShadowColor - 文字陰影顏色
#UITextAttributeTextShadowOffset - 偏移用于文本陰影
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
// 字典有點(diǎn)長(zhǎng)鞋邑,將就看一下哈
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName, shadow, NSShadowAttributeName, [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil nil]];
#使用圖片作為導(dǎo)航欄標(biāo)題
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];
#添加多個(gè)欄按鈕項(xiàng)目
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action: nil nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action: nil nil];
NSArray *itemsArr = @[shareItem,cameraItem]; self.navigationItem.rightBarButtonItems = itemsArr;
#自定義后退按鈕的文字和顏色
方法一:
通過(guò)設(shè)置navigationItem的backBarButtonItem可以直接更換文字,【注意账蓉,要在父視圖的Controller中設(shè)置】如代碼:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
效果:所有的子界面返回時(shí)都變成了我們定義的文字枚碗,如果不想顯示文字,直接""铸本,就會(huì)單獨(dú)顯示一個(gè)系統(tǒng)的返回箭頭圖標(biāo)肮雨,也是很清晰的感覺(jué)。
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
效果:全是系統(tǒng)的圖標(biāo)和文字箱玷,【除了后退按鈕怨规,請(qǐng)注意,tintColor屬性影響所有按鈕標(biāo)題和按鈕圖像】
#另外一種實(shí)現(xiàn)自定義導(dǎo)航控制器返回按鈕锡足,代碼如下:
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
self.title=[NSString stringWithFormat:@"第%lu頁(yè)",(unsigned long)self.navigationController.viewControllers.count];
//自定義返回按鈕
UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
##//將返回按鈕的文字position設(shè)置不在屏幕上顯示##
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];