1.預置按鈕類型
sdk提供了5個預置按鈕類型:Detail Disclosure,Info Light,Info Dark,Contact Add,Rounded Rectangle。它們添加到sdk中首先是為了方便蘋果公司自己。
構造方式:[UIButton buttonWithType:UIButtonTypeContactAdd];
2.顯示系統私有UIButton風格
指定 值為100 以上的UIButton的buttonWithType可以得到非公開的按鈕風格,像紅色按鈕,黑色按鈕,箭頭返回按鈕等。
對於某種風格,可以用[button setTintColor:[UIColor blueColor]];來改變按鈕顏色。
參考
http://zhaohaiyang.blog.51cto.com/2056753/756082
3.圖片和文字環繞
UIButtonTypeCustom按鈕可以設置title。
若置title於圖像上面時,可使用setBackgroundImage;
若置title於圖像右邊時,可使用setImage,且要設置frame寬度大於圖像,以能顯示出title文字。
設置titleEdgeInsets可實現文字到圖片下方,不過要經過一翻計算。
setImage的圖的Z坐標是最高的。
4.光暈效果
button.showsTouchWhenHighlighted=YES;點擊時的閃光效果會被前景圖片遮住中間部分;
Shows Touch On Highlight (高亮)光暈的大小是55x55像素,大於40x40像素的按鈕不能使用該視覺效果。
5.指定目標函數傳遞的參數問題
例如
[button addTarget:self action:@selector(tableView:accessoryButtonTappedForRowWithIndexPath:) forControlEvents:UIControlEventTouchUpInside];,
在執行時,傳遞給tableView函數的參數類型分別是UIButton類型和UITouchesEvent類型。即不論函數原型是什么,button實際傳遞的參數類型是固定的。
6.點擊測試UIButton響應UIControlEventTouchUpInside事件時,響應點超出了它button的范圍。
7.在UIButton中addSubview的問題
UIView的userInteractionEnabled值默認為YES,必須設置UIButton所有的subview的userInteractionEnabled為NO,才能讓UIButton正常響應點擊。
但是如果設置了UIView的setUserInteractionEnabled為NO,其子view都將得不到響應。
8.處理雙擊問題
[button addTarget:self action:@selector(onTouchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void)onTouchUpInside:(id)sender withEvent:(UIEvent*)event
{
UITouch* touch = [[event allTouches] anyObject];
NSLog(@"onTouchUpInside tagCount:%d",touch.tapCount);
//判斷點擊次數
if (touch.tapCount == 1)
{
//todo
}
}