clipsToBounds vs masksToBounds
clipsToBounds
clipsToBounds 決定子視圖的顯示范圍:設(shè)置為YES時(shí),子視圖超出部分將被剪裁碉纳,不會顯示适室;設(shè)置為NO則不會剪裁嫡意。
clipsToBounds的默認(rèn)值為NO,但是在UIScrollview中為YES捣辆。
比如view2添加到view1上蔬螟,即view2為view1的subview。
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 100, 100)];
view1.backgroundColor = [UIColor blueColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
view2.backgroundColor = [UIColor redColor];
[view1 addSubview:view2];
view1的clipsToBounds設(shè)置為NO時(shí)汽畴,view2超出view1的部分將不會剪裁旧巾,如圖左耸序,紅色部分不會被剪裁。
view1.clipsToBounds = NO;
view1的clipsToBounds設(shè)置為YES時(shí)菠齿,view2超出view1的部分將會被剪裁佑吝,如圖右,紅色部分被剪掉了绳匀。
view1.clipsToBounds = YES;
在Apple的Prefetching Collection View Data 示例代碼中就是將UICollectionView 的 clipsToBounds 設(shè)置為 NO以顯示 cell 超出 UICollectionView 時(shí)的狀態(tài)芋忿,來觀察UICollectionViewCell的生命周期。
masksToBounds
masksToBounds
的功能和clipsToBounds
類似疾棵,但是clipsToBounds
是CALayer的屬性戈钢,clipsToBounds
是UIView新的屬性,clipsToBounds
會調(diào)用maskToBounds
方法是尔。
通常使用在設(shè)置cornerRadius不能達(dá)到圓角效果的控件上殉了,如UIImageView、UILabel等拟枚。
imageView.layer.cornerRadius = 5.f;
imageView.layer.masksToBounds = YES;
需要注意薪铜,設(shè)置maskToBounds = YES
可能會觸發(fā)離屏渲染。關(guān)于可參考關(guān)于iOS離屏渲染的深入研究恩溅。
UITextField && UITextView 輸入限制
限制文本輸入可以監(jiān)聽UIKeyboardWillChangeFrameNotification
通知隔箍,在其中判斷文字長度。但需要注意的是:不要把高亮的部分脚乡,即聯(lián)想輸入的部分記入到字?jǐn)?shù)統(tǒng)計(jì)中蜒滩,因?yàn)檫@部分不是我們真正要輸入的內(nèi)容。比如在中文情況下奶稠,輸入拼音時(shí)俯艰,還沒有選中文字就會被鍵盤當(dāng)作字母輸入到UITextField/UITextView中,比如可輸入字符還剩下1個(gè)锌订,此時(shí)想打一個(gè)“吃”字竹握,輸入拼音“chi”,則會將計(jì)算的是“chi”三個(gè)字符的長度辆飘。
- (void)textViewDidChange:(UITextView *)textView {
if (textView.text.length > self.maxInputWords && !textView.markedTextRange) { // 防止把高亮的部分計(jì)入
textView.text = [textView.text substringToIndex:self.maxInputWords];
}
// 顯示已經(jīng)輸入文字個(gè)數(shù)
self.wordsLabel.text = [NSString stringWithFormat:@"%lu/%ld", (long)textView.text.length, (long)self.maxInputWords];
}
UITextView return鍵隱藏鍵盤
UITextView 不像 UITextfield 一樣提供了 textFieldShouldReturn:
方法實(shí)現(xiàn)涩搓,可以通過 UITextViewDelegate 中的textView:shouldChangeTextInRange:replacementText
代理方法實(shí)現(xiàn):
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]){ // 判斷輸入的字是否是回車,即按下 return
[textView resignFirstResponder];
return NO;
}
return YES;
}
UISwitch 開關(guān)保持原來的狀態(tài)
UISwitch在點(diǎn)擊時(shí)狀態(tài)即會改變劈猪,如果想讓它點(diǎn)擊時(shí)不馬上改變狀態(tài),而是進(jìn)行其他操作之后再響應(yīng)良拼,可以在UISwitch的target方法中這樣操作:
[switch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
- (void)switchAction:(UISwitch *)sender {
// 讓開關(guān)狀態(tài)不變
[sender setOn:!sender.on animated:YES];
// 其他操作...
// 修改開關(guān)狀態(tài)
sender.on = ...;
}
注意:是把a(bǔ)nimated設(shè)置為YES战得。原因推測可能是animated有延時(shí)。
UITableView 單選多選實(shí)現(xiàn)
UITableview提供了單選和多選機(jī)制庸推,分別是allowsSelection
和allowsMultipleSelection
屬性常侦,但是當(dāng)你想要在一個(gè)TabelView的不同section中分別使用單選和多選(比如第一個(gè)section支持單選浇冰,第二個(gè) section 支持多選)就需要自己實(shí)現(xiàn)了。在 section 不多的情況下聋亡,這里提供一種快速的方案:讓 UITableview 開啟多選肘习,即allowsMultipleSelection=YES
,用變量記錄單選section中上一次選中的行坡倔,在tableView:didSelectRowAtIndexPath
中進(jìn)行判斷漂佩,如果選擇的是不同行,則取消上一次選中罪塔。
self.tableView.allowsMultipleSelection = YES; // 允許多選
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BBASuggestFeedbackHeaderModel *headerModel;
if (indexPath.section == 0) {
// 取消上一次選中
if (self.lastSelectedIndex != indexPath.row) { // 點(diǎn)擊同一個(gè)不做處理
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.lastSelectedIndex inSection:indexPath.section];
[tableView deselectRowAtIndexPath:lastIndex animated:YES];
self.lastSelectedIndex = indexPath.row;
}
}
}
獲取選中狀態(tài)的cell投蝉,即可通過indexPathsForSelectedRows
獲取。
UICollectionViewCell 高亮和點(diǎn)擊態(tài)
UICollectionView不像UITableView一樣默認(rèn)有高亮狀態(tài)征堪,可以通過設(shè)置 UICollectionViewCell 的selectedBackgroundView實(shí)現(xiàn)瘩缆。并且UICollectionView也提供幾個(gè)高亮的代理方法
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
但是如果這個(gè)時(shí)候的需求是需要在高亮的時(shí)候讓cell上的其他子控件也改變alph值,單純的設(shè)置selectedBackgroundView的不能滿足佃蚜。
可以通過重寫Cell的兩個(gè)方法setHighlighted:
和setSelected:
庸娱,
-
setHighlighted:
高亮狀態(tài),即按在cell上不松開的效果 -
setSelected:
選中狀態(tài)谐算,點(diǎn)擊一個(gè)cell即選中
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
// 高亮狀態(tài)下的子控件顏色設(shè)置
} else {
// 普通狀態(tài)的子控件顏色設(shè)置
}
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
// 高亮狀態(tài)下的子控件顏色設(shè)置
} else {
// 普通狀態(tài)的子控件顏色設(shè)置
}
}
Cell上UIImageView的圖片不顯示
如果cell上的UIImageView在不同情況下會size不同熟尉,圓角不同,cell復(fù)用是需要更新UIImageView的約束氯夷。遇到UIImageView中圖片不顯示的情況臣樱,可以從以下幾個(gè)方面排查:
- imageView 的 frame 是否設(shè)置正確,比如采用
initWithImage:
方法初始化時(shí)腮考,會根據(jù)圖片的大小來設(shè)置imageView的frame可以不用初始化尺寸雇毫,但是大小不可控。 - image view 的 hidden 是否設(shè)置
- 是否添加到父 view 上
- 是否設(shè)置 imageView 的 image
- imageView 的 image 是否是從網(wǎng)絡(luò)獲取的踩蔚,是否采用占位圖片
- imageView是否在設(shè)置圓角棚放,圓角是否過大,比如ImageView的size為20 * 20馅闽,而圓角大小為40飘蚯,則不會顯示。