在 UICollectionView 的 cell 中添加 subView 不显示的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
tsinghan
V2EX    iDev

在 UICollectionView 的 cell 中添加 subView 不显示的问题

  •  
  •   tsinghan 2015-01-29 21:42:36 +08:00 8571 次点击
    这是一个创建于 3921 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在cellForItemAtIndexPath方法中

    var videoView = videos["video"] as UIView videoView.frame = cell.frame self.collectionView.addSubview(videoView) 

    如果我设置了 videoView.frame = cell.frame 这句,那么这个subview 就不会显示

    我修改了frame(包含在cell中)

    var videoView = videos["video"] as UIView videoView.frame = videoView.frame = CGRect(x: 37, y: 94, width: 100, height: 100) self.collectionView.addSubview(videoView) 

    这样就显示了

    请问这是什么原因?

    9 条回复    2015-01-29 23:44:41 +08:00
    krafttuc
        1
    krafttuc  
       2015-01-29 22:36:49 +08:00
    没看明白楼主为什么要在 collection view 里添加这些个 video view,而不是把他们放到 cell 里。
    keithellis
        2
    keithellis  
       2015-01-29 22:39:17 +08:00
    自定义一个 UICollectionViewCell,在 layoutSubviews 方法中设置 frame,另外 subView 我一般放在 contentView 里。
    mailworks
        3
    mailworks  
       2015-01-29 22:57:13 +08:00
    如楼上,自定义的View添加到UICollectionViewCell的contentView 中,
    videoView.frame = cell.bounds这样设置
    ruandao
        4
    ruandao  
       2015-01-29 23:04:44 +08:00
    把frame 打印出来~~
    tsinghan
        5
    tsinghan  
    OP
       2015-01-29 23:11:53 +08:00
    @krafttuc
    @keithellis 抱歉我贴错代码了 我是放到 contentView里的 cell.contentView.addSubview(videoView)
    tsinghan
        6
    tsinghan  
    OP
       2015-01-29 23:12:24 +08:00
    @mailworks 恩 我试试这种方法
    tsinghan
        7
    tsinghan  
    OP
       2015-01-29 23:26:51 +08:00
    @krafttuc
    @keithellis
    @ruandao
    @mailworks 恩 用videoView.frame = cell.bounds 这种方式设置确实可以显示了
    我分别打印了 cell.bounds 和 cell.frame 分别为 (0.0,0.0,155.0,125.0) 和 (0.0,230.0,155.0,125.0)
    而我cell的大小就是 (0.0,230.0,155.0,125.0) 这样的, 为什么要videoView.frame = cell.bounds 设置?没搞懂
    vincentxue
        8
    vincentxue  
       2015-01-29 23:32:20 +08:00
    我靠 我刚发现我写的回复怎么没出来。。。。。
    重新贴一下吧还好我有剪贴板历史记录。

    如果你一定要在这里添加子视图,那坐标应该这么写:

    videoView.frame = cell.bounds;

    如果是 tableView 要在 willDisplayCell 中设置 frame,因为 UITableViewCell 从初始化到显示之前都不知道自己的 frame 的,默认都是 CGRectZero。

    但自定义 cell 通常应该子类化。

    frame 是相对于父视图而言的,

    假设有一个 UICollectionView, frame 为 {{0, 0}, {320, 480}};
    假设 cell 大小是 {40, 40},间隙为 0, 0 的话,那么你屏幕上显示的第一行(8 个)的 cell 的 frame 应该如下:

    0: {{0, 0}, {40, 40}}
    1: {{40, 0}, {40, 40}}
    2: {{80, 0}, {40, 40}}
    3: {{120, 0}, {40, 40}}
    4: {{160, 0}, {40, 40}}
    5: {{200, 0}, {40, 40}}
    6: {{240, 0}, {40, 40}}
    7: {{280, 0}, {40, 40}}

    你可以想象一下,如果你的父视图才 {40, 40},你自身的 frame.origin.x 或者 y 就超过 40 了,肯定是看不见的了。
    tsinghan
        9
    tsinghan  
    OP
       2015-01-29 23:44:41 +08:00
    @vincentxue 明白了 十分感谢
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     997 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 18:52 PVG 02:52 LAX 11:52 JFK 14:52
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86