我创建了一个自定义UICollectionViewCell
,其中包含标签的导出(放置在Storyboard中)。我想从我的自定义awakeFromNib
的UICollectionViewCell
方法中获取此标签的高度,但标签的大小始终为0.000000
:
// .h
@interface MyCustomCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
// .m
@implementation MyCustomCell
-(void)awakeFromNib
{
[super awakeFromNib];
NSLog(@"%@", self.myLabel);
NSLog(@"%f", self.myLabel.frame.size.width);
NSLog(@"%f", self.myLabel.frame.size.height);
}
@end
控制台输出:
2013-02-06 11:13:59.628 Test[8880:c07] <UILabel: 0x7649a00; frame = (0 0; 0 0); text = '12h00'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7649ac0>>
2013-02-06 11:13:59.628 Test[8880:c07] 0.000000
2013-02-06 11:13:59.630 Test[8880:c07] 0.000000
如何获得标签的尺寸?
在调用
awakeFromNib
时获取此类信息是否为时过早?如果是这样,我应该使用哪种自定义单元格的方法来获取大小?编辑
这是在ViewController中观察到的相同的奇怪行为:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCustomCell" forIndexPath:indexPath];
NSLog(@"%@", cell);
NSLog(@"%@", cell.myLabel);
}
并输出:
2013-02-07 16:07:34.488 Test[30308:c07] <MyCustomCell: 0x7156980; baseClass = UICollectionViewCell; frame = (20 0; 290 655); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7156a80>>
2013-02-07 16:07:34.489 Test[30308:c07] <UILabel: 0x7158100; frame = (0 0; 0 0); text = 'this is a text'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7158040>>
最佳答案
答案的一部分是dreamzor对iOS AutoLayout - get frame size width的响应。
在我的特殊情况下,我发现在要求导出尺寸之前,在自定义单元格[self layoutIfNeeded];
方法中添加awakeFromNib
就像是一种魅力。
关于ios - 在awakeFromNib中获得 socket 尺寸,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14726810/