clipsToBounds vs masksToBounds
clipsToBounds
ClipsToBounds determines the range of subviews to display. If set to YES, portions of the subview that exceed the range are clipped and not displayed. If set to NO, clipping is not done. ClipsToBounds defaults to NO, but is YES in UIScrollview.
For example, view2 is added to view1, so view2 is view1’s 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];
Copy the code
When view1 clipsToBounds is set to NO, the portion of view2 beyond view1 is not clipped, and the red portion is not clipped.
view1.clipsToBounds = NO;
Copy the code
When view1 clipsToBounds is set to YES, the portion of view2 beyond view1 will be clipped, with the red portion cut out on the right.
view1.clipsToBounds = YES;
Copy the code
In Apple’s sample code for Prefetching CollectionView Data, clipsToBounds of UICollectionView was set to NO to indicate that the cell is out of the UICollectionView To observe the lifecycle of the UICollectionViewCell.
masksToBounds
The function of masksToBounds is similar to that of clipsToBounds, but clipsToBounds is a CALayer property. ClipsToBounds is a new UIView property.
Usually used for cornerRadius controls that do not have a corner effect, such as UIImageView, UILabel, etc.
imageView.layer.cornerRadius = 5.f;
imageView.layer.masksToBounds = YES;
Copy the code
Note that setting maskToBounds = YES may trigger an off-screen rendering. For more information, see an in-depth look at iOS off-screen rendering.
UITextField && UITextView input limits
Limit text input can listen UIKeyboardWillChangeFrameNotification notice, in which judgment text length. One caveat: Don’t include the highlighted, associative input in your word count, because that’s not what you’re really typing. For example, in the case of Chinese, input pinyin, has not selected the text will be keyboard as a letter input to UITextField/UITextView, such as the input character is still 1, want to type a “eat” word, input pinyin “chi”, will calculate the length of “chi” three characters.
- (void)textViewDidChange:(UITextView *)textView {
if (textView.text.length > self.maxInputWords && ! textView.markedTextRange) {// Avoid counting highlighted parts
textView.text = [textView.text substringToIndex:self.maxInputWords];
}
// Displays the number of entered characters
self.wordsLabel.text = [NSString stringWithFormat:@"%lu/%ld", (long)textView.text.length, (long)self.maxInputWords];
}
Copy the code
UITextView Return key hides the keyboard
Unlike UITextView, which provides a textFieldShouldReturn method implementation, Can pass the textView UITextViewDelegate: shouldChangeTextInRange: replacementText proxy method implementation:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {// Determine if the input is a carriage return, that is, press return
[textView resignFirstResponder];
return NO;
}
return YES;
}
Copy the code
The UISwitch switch remains in the original state
The UISwitch state changes when it is clicked. If you want the UISwitch state to change after the click, you can do this in the Target method of UISwitch:
[switch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
- (void)switchAction:(UISwitch *)sender {
// Leave the switch state unchanged
[sender setOn:!sender.on animated:YES];
// Other operations...
// Change the switch statussender.on = ... ; }Copy the code
Note: Set animated to YES. The reason may be that animated has a delay.
UITableView single option multiple implementation
UITableview provides single and multiple selection mechanisms with the allowsSelection and allowsMultipleSelection properties, respectively. However, when you want to use radio and multiple selections in different sections of a TabelView (for example, the first section supports radio and the second section supports multiple selections), you need to implement it yourself. Here’s a quick solution for a limited number of sections: Let UITableview open alternative, namely allowsMultipleSelection = YES, recorded in variable radio section in the last line, selected in the tableView: didSelectRowAtIndexPath of judgment, If a peer is not selected, the previous selection is deselected.
self.tableView.allowsMultipleSelection = YES; // allow multiple selections - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { BBASuggestFeedbackHeaderModel *headerModel;if(indexPath. Section == 0) {// Deselect the last checkif(self.lastSelectedIndex ! = indexPath. Row) {/ / click the same don't do processing NSIndexPath * lastIndex = [NSIndexPath indexPathForRow: self. LastSelectedIndexinSection:indexPath.section]; [tableView deselectRowAtIndexPath:lastIndex animated:YES]; self.lastSelectedIndex = indexPath.row; }}}Copy the code
Obtain the selected cell, can be obtained through indexPathsForSelectedRows.
UICollectionViewCell highlights and clicks
UICollectionView does not have a highlighted state by default like a UITableView. This can be done by setting the selectedBackgroundView of the UICollectionViewCell. And UICollectionView also provides several highlighted proxy methods
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
However, if the need is to change the ALpH of the other child controls on the cell while highlighting, simply setting the selectedBackgroundView is not enough. You can override the Cell by using two methods setHighlighted and setSelected:
setHighlighted:
Highlighted state, which is the effect of pressing the cell without releasing itsetSelected:
Select state, click a cell to select
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
// Set the color of the child control in the highlighted state
} else {
// Normal state child control color Settings}} - (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
// Set the color of the child control in the highlighted state
} else {
// Normal state child control color Settings}}Copy the code
The UIImageView image on the Cell is not displayed
If the UIImageView on the cell has different sizes and rounded corners in different cases, cell reuse is a constraint that requires updating the UIImageView. UIImageView does not display images in UIImageView.
- Whether the frame of the imageView is set correctly, such as using
initWithImage:
Method will set the frame of the imageView based on the size of the image. You don’t need to initialize the size, but the size is not controllable. - The hidden of the image View is set
- Whether to add it to the parent view
- Whether to set the image of the imageView
- Whether the imageView image is fetched from the network, and whether it uses a placeholder image
- Whether the imageView is setting the rounded corner and whether the rounded corner is too large. For example, if the imageView size is 20 * 20 and the rounded corner size is 40, it will not be displayed.