国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

addSubview和insertSubview 區別

2019-11-09 16:14:53
字體:
來源:轉載
供稿:網友

子視圖是以棧的方式存放的,也就是說有層次的存放 addSubview: addsubview時都是在最上面面添加 eg:

UIView *v1 = [UIView new]; v1.frame = CGRectMake(100, 100, 100, 60); v1.backgroundColor = [UIColor redColor]; UIView *v2 = [UIView new]; v2.backgroundColor = [UIColor purpleColor]; v2.frame = CGRectMake(80, 120, 100, 60); [self.view addSubview:v1]; [self.view addSubview:v2];

看到如下效果 addSubview

insertSubview insertSubView可以控制將view添加到指定的層 eg:

UIView *v1 = [UIView new]; v1.frame = CGRectMake(100, 100, 100, 60); v1.backgroundColor = [UIColor redColor]; UIView *v2 = [UIView new]; v2.backgroundColor = [UIColor purpleColor]; v2.frame = CGRectMake(80, 120, 100, 60); [self.view addSubview:v1]; [self.view addSubview:v2]; UIView *v3 = [UIView new]; v3.backgroundColor = [UIColor yellowColor]; v3.frame = CGRectMake(60, 140, 160, 60); [self.view insertSubview:v3 atIndex:3];

看到如下效果: insertSubview 可以看到v3在v1跟v2之間,按照我們的打算,atIndex:3 應該是在第四層,而現在感覺不像,我們可以打印一下看下究竟:

NSLog(@"%@",self.view.subviews);

打印如下:

2017-02-08 10:10:07.310 sa1[1058:39734] ( "<_UILayoutGuide: 0x7f8893602e80; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x60800003e220>>", "<_UILayoutGuide: 0x7f8893609080; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x60800003e080>>", "<UIView: 0x7f8893406ab0; frame = (100 100; 100 60); layer = <CALayer: 0x60000003de40>>", "<UIView: 0x7f8893407270; frame = (60 140; 160 60); layer = <CALayer: 0x60000003dc00>>", "<UIView: 0x7f8893407c60; frame = (80 120; 100 60); layer = <CALayer: 0x60000003de60>>")

可以看到v1之前,也就是self.view有兩層,v1 index就是2, v2的為4,v3 插在 index:3 ,所以在v1跟v2之間。

可以看到: [self.view addSubView:xx.view] 其實就等于[self.view insertSubView:xx.view atIndex:[self.view.subViews count]]; 即在最頂層添加view。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 偏关县| 烟台市| 神池县| 电白县| 远安县| 正定县| 大关县| 五寨县| 罗山县| 大新县| 吉安市| 紫金县| 邻水| 永福县| 潢川县| 关岭| 莲花县| 武定县| 布拖县| 辉南县| 内江市| 丰原市| 宜丰县| 上蔡县| 万全县| 广德县| 都兰县| 金阳县| 绥阳县| 商水县| 营口市| 和龙市| 汶上县| 靖宇县| 两当县| 罗源县| 鄱阳县| 内黄县| 临澧县| 迁西县| 宜川县|