iOS-UI控件精講之UIView

道雖邇,不行不至;事雖小,不為不成。

相關(guān)閱讀
1.iOS-UI控件精講之UIView(本文)
2.iOS-UI控件精講之UILabel
...待續(xù)

UIView是所有UI控件的基類(lèi),在布局的時(shí)候通常會(huì)使用UIView作為容器對(duì)控件進(jìn)行分組。

1.首先看一下所有的UI控件的繼承關(guān)系

UIView繼承關(guān)系

UIView中的屬性和方法定義了所有的UI控件的公共行為,UIView中所有的public屬性,你在它的所有的子控件中都可以使用的。

2.UIView中常見(jiàn)的屬性和方法

2.1幾何相關(guān)

//這幾個(gè)屬性都支持隱式動(dòng)畫(huà)的
@property(nonatomic) CGRect            frame;//view的相對(duì)于父控件的位置(x,y)和大小(width,height)
@property(nonatomic) CGRect            bounds; //view的相對(duì)于自身的位置(x,y)和大小(width,height)    (x,y)一般為(0,0)
@property(nonatomic) CGPoint           center;//view的中心點(diǎn)相對(duì)于父控件的位置
@property(nonatomic) CGAffineTransform transform; //view的形變屬性
frame和center的區(qū)別
//添加一個(gè)view并設(shè)置紅色背景色
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake(150, 300, 100, 100);
//設(shè)置旋轉(zhuǎn)45度的形變屬性
view.transform = CGAffineTransformRotate(view.transform, M_PI_4);
[self.view addSubview:view];

2.2視圖從屬關(guān)系

@property(nullable, nonatomic,readonly) UIView       *superview;//所屬父類(lèi)
@property(nonatomic,readonly,copy) NSArray<__kindof UIView *>*subviews;//所有的子類(lèi)
@property(nullable, nonatomic,readonly) UIWindow     *window;//所屬的window

- (void)removeFromSuperview;//從父類(lèi)中移除
- (void)addSubview:(UIView *)view;//添加子類(lèi)
- (nullable __kindof UIView *)viewWithTag:(NSInteger)tag;//通過(guò)tag搜索子view

2.3其他

@property(nonatomic)                                 NSInteger tag;//設(shè)置tag,主要是為了跟別的view進(jìn)行區(qū)分
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;//是否允許交互

@property(nonatomic,readonly,strong)                 CALayer  *layer;//view的圖層,view顯示的內(nèi)容layer屬性決定的。

下面看這個(gè)layer的例子

UIView *view = [[UIView alloc] init];
//這里我已經(jīng)設(shè)置了backgroundColor為redColor
view.backgroundColor = [UIColor redColor];
//在這里我又設(shè)置了layer的backgroundColor為brownColor
view.layer.backgroundColor = [UIColor brownColor].CGColor;

view.frame = CGRectMake(150, 300, 100, 100);
[self.view addSubview:view];
最終的顯示效果

關(guān)于layer的應(yīng)用還有兩個(gè)比較常見(jiàn)的

//1.圓角
view.layer.cornerRadius = 10;//后面的這個(gè)值越大就越圓,等寬高的view的寬度的一半就是一個(gè)圓形
設(shè)置圓角
設(shè)置圓角
設(shè)置邊框
view.layer.cornerRadius = view.bounds.size.width / 2;
//設(shè)置邊框的寬度
view.layer.borderWidth = 3;
//設(shè)置邊框的顏色
view.layer.borderColor = [UIColor blueColor].CGColor;
圓形帶藍(lán)色邊框
圓形帶藍(lán)色邊框

clipsToBounds屬性

@property(nonatomic)                 BOOL              clipsToBounds;//超出邊框是否剪切

我們先看看超出邊框的情況

//紅色view
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake(150, 300, 100, 100);
[self.view addSubview:view];
//往紅色的view上面添加棕色view  
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor brownColor];
v.frame = CGRectMake(50, 50, 100, 100);
[view addSubview:v];
//設(shè)置超出邊框裁剪為Yes
view.clipsToBounds = YES;
設(shè)置clipsToBounds前后對(duì)比

本文適合iOS開(kāi)發(fā)初學(xué)者閱讀,大牛們請(qǐng)一笑而過(guò),如果有錯(cuò)誤請(qǐng)聯(lián)系我 。
如果您喜歡這篇文章,請(qǐng)關(guān)注我,喜歡或者打賞!您的支持十分重要!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容