今天在開發終于到了子視圖超出父視圖的部分不顯示。然后就出現了這個<pre>ClipsToBounds</pre>屬性了。
簡單的說就是,當<pre>ClipsToBounds</pre>屬性設置成YES的時,剪裁超出父視圖范圍的子視圖部分。當設置成NO的時候,不剪裁超出父視圖范圍的子視圖。
默認是NO 在scrollView中默認是YES
舉個例子:
下面是紅色view是藍色view的子視圖 ,父視圖(藍色view)的<pre>ClipsToBounds</pre>設置成NO;
<pre>
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30,200, 200, 40)];
[self.view addSubview:textField];
textField.backgroundColor = [UIColor blueColor];
textField.clipsToBounds = NO;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 3000, 4)];
[textField addSubview:label];
label.backgroundColor = [UIColor redColor];
</pre>
效果圖如下:
ClipsToBounds設置成YES;
<pre>
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30,200, 200, 40)];
[self.view addSubview:textField];
textField.backgroundColor = [UIColor blueColor];
textField.clipsToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 3000, 4)];
[textField addSubview:label];
label.backgroundColor = [UIColor redColor];
</pre>
效果圖如下: