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

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

addSubview和insertSubview 區別

2019-11-09 17:19:58
字體:
來源:轉載
供稿:網友

子視圖是以棧的方式存放的,也就是說有層次的存放 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。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 土默特左旗| 炎陵县| 普安县| 昆明市| 通渭县| 靖边县| 拜泉县| 赣榆县| 思茅市| 九台市| 桃江县| 那曲县| 边坝县| 蚌埠市| 三明市| 甘德县| 昂仁县| 黄梅县| 广河县| 松溪县| 鄱阳县| 佳木斯市| 综艺| 内江市| 镇赉县| 乡城县| 天长市| 宁乡县| 大英县| 崇信县| 图木舒克市| 西吉县| 宁城县| 湘西| 宝鸡市| 松潘县| 定州市| 荣成市| 鹤山市| 通榆县| 柞水县|