gpt4 book ai didi

ios - 在 awakeFromNib 中获取导出尺寸

转载 作者:可可西里 更新时间:2023-11-01 03:37:52 25 4
gpt4 key购买 nike

我创建了一个自定义 UICollectionViewCell,其中包含一个标 checkout 口(放置在 Storyboard 中)。我想从自定义 UICollectionViewCellawakeFromNib 方法中获取此标签的高度,但标签的大小始终为 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 的回复中.

The trick is to place your frame-dependent code to the viewDidLayoutSubviews method

在我的特殊情况下,我发现在我的自定义单元格 awakeFromNib 方法中添加 [self layoutIfNeeded];,就在询问导出尺寸之前,效果非常好。

关于ios - 在 awakeFromNib 中获取导出尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14726810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com