clipsToBounds 决定子视图的显示范围:设置为YES时,子视图超出部分将被剪裁,不会显示;设置为NO则不会剪裁。 clipsToBounds的默认值为NO,可是在UIScrollview中为YES。bash
好比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设置为NO时,view2超出view1的部分将不会剪裁,如图左,红色部分不会被剪裁。app
view1.clipsToBounds = NO;
复制代码
view1的clipsToBounds设置为YES时,view2超出view1的部分将会被剪裁,如图右,红色部分被剪掉了。fetch
view1.clipsToBounds = YES;
复制代码
在Apple的Prefetching Collection View Data 示例代码中就是将UICollectionView 的 clipsToBounds 设置为 NO以显示 cell 超出 UICollectionView 时的状态,来观察UICollectionViewCell的生命周期。ui
masksToBounds
的功能和clipsToBounds
相似,可是clipsToBounds
是CALayer的属性,clipsToBounds
是UIView新的属性,clipsToBounds
会调用maskToBounds
方法。spa
一般使用在设置cornerRadius不能达到圆角效果的控件上,如UIImageView、UILabel等。代理
imageView.layer.cornerRadius = 5.f;
imageView.layer.masksToBounds = YES;
复制代码
须要注意,设置maskToBounds = YES
可能会触发离屏渲染。关于可参考关于iOS离屏渲染的深刻研究。code
限制文本输入能够监听UIKeyboardWillChangeFrameNotification
通知,在其中判断文字长度。但须要注意的是:不要把高亮的部分,即联想输入的部分记入到字数统计中,由于这部分不是咱们真正要输入的内容。好比在中文状况下,输入拼音时,尚未选中文字就会被键盘看成字母输入到UITextField/UITextView中,好比可输入字符还剩下1个,此时想打一个“吃”字,输入拼音“chi”,则会将计算的是“chi”三个字符的长度。orm
- (void)textViewDidChange:(UITextView *)textView {
if (textView.text.length > self.maxInputWords && !textView.markedTextRange) { // 防止把高亮的部分计入
textView.text = [textView.text substringToIndex:self.maxInputWords];
}
// 显示已经输入文字个数
self.wordsLabel.text = [NSString stringWithFormat:@"%lu/%ld", (long)textView.text.length, (long)self.maxInputWords];
}
复制代码
UITextView 不像 UITextfield 同样提供了 textFieldShouldReturn:
方法实现,能够经过 UITextViewDelegate 中的textView:shouldChangeTextInRange:replacementText
代理方法实现:cdn
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]){ // 判断输入的字是不是回车,即按下 return
[textView resignFirstResponder];
return NO;
}
return YES;
}
复制代码
UISwitch在点击时状态即会改变,若是想让它点击时不立刻改变状态,而是进行其余操做以后再响应,能够在UISwitch的target方法中这样操做:
[switch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
- (void)switchAction:(UISwitch *)sender {
// 让开关状态不变
[sender setOn:!sender.on animated:YES];
// 其余操做...
// 修改开关状态
sender.on = ...;
}
复制代码
注意:是把animated设置为YES。缘由推测多是animated有延时。
UITableview提供了单选和多选机制,分别是allowsSelection
和allowsMultipleSelection
属性,可是当你想要在一个TabelView的不一样section中分别使用单选和多选(好比第一个section支持单选,第二个 section 支持多选)就须要本身实现了。在 section 很少的状况下,这里提供一种快速的方案:让 UITableview 开启多选,即allowsMultipleSelection=YES
,用变量记录单选section中上一次选中的行,在tableView:didSelectRowAtIndexPath
中进行判断,若是选择的是不一样行,则取消上一次选中。
self.tableView.allowsMultipleSelection = YES; // 容许多选
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BBASuggestFeedbackHeaderModel *headerModel;
if (indexPath.section == 0) {
// 取消上一次选中
if (self.lastSelectedIndex != indexPath.row) { // 点击同一个不作处理
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.lastSelectedIndex inSection:indexPath.section];
[tableView deselectRowAtIndexPath:lastIndex animated:YES];
self.lastSelectedIndex = indexPath.row;
}
}
}
复制代码
获取选中状态的cell,便可经过indexPathsForSelectedRows
获取。
UICollectionView不像UITableView同样默认有高亮状态,能够经过设置 UICollectionViewCell 的selectedBackgroundView实现。而且UICollectionView也提供几个高亮的代理方法
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
可是若是这个时候的需求是须要在高亮的时候让cell上的其余子控件也改变alph值,单纯的设置selectedBackgroundView的不能知足。 能够经过重写Cell的两个方法setHighlighted:
和setSelected:
,
setHighlighted:
高亮状态,即按在cell上不松开的效果setSelected:
选中状态,点击一个cell即选中- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
// 高亮状态下的子控件颜色设置
} else {
// 普通状态的子控件颜色设置
}
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
// 高亮状态下的子控件颜色设置
} else {
// 普通状态的子控件颜色设置
}
}
复制代码
若是cell上的UIImageView在不一样状况下会size不一样,圆角不一样,cell复用是须要更新UIImageView的约束。遇到UIImageView中图片不显示的状况,能够从如下几个方面排查:
initWithImage:
方法初始化时,会根据图片的大小来设置imageView的frame能够不用初始化尺寸,可是大小不可控。