【问题标题】:Confusion about UIButton's titleLabel and imageView property关于 UIButton titleLabel 和 uiimageView 属性的混淆
【发布时间】:2015-12-03 16:41:06
【问题描述】:

我有两个问题:

  • 为什么button.titleLabel.center等于button.titleLabel.origin,而
  • 为什么imageView.frame 为零?

        UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(100, 200, 200, 100);
        button.backgroundColor=[UIColor whiteColor];
        [button setTitle:@"\U0000e602" forState:UIControlStateNormal];
        button.font=iconfont;
        [button setBackgroundImage:[UIImage imageNamed:@"pig.jpg"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.view addSubview:button];
    
         NSLog(@"button.titleLabel.frame%@",NSStringFromCGRect(button.titleLabel.frame));
    
         NSLog(@"button.frame%@",NSStringFromCGRect(button.frame));
         NSLog(@"button.titleLabel.origin%@\nbutton.titleLabel.center%@",NSStringFromCGPoint(button.titleLabel.frame.origin),NSStringFromCGPoint(button.titleLabel.center));
         NSLog(@"button.imageView.frame%@",NSStringFromCGRect(button.imageView.frame));
    

结果流淌:

2015-09-08 13:27:43.507 IconFontTest[3458:152384] button.titleLabel.frame{{100, 50}, {0, 0}}
2015-09-08 13:27:43.508 IconFontTest[3458:152384] button.frame{{100, 200}, {200, 100}}
2015-09-08 13:27:43.508 IconFontTest[3458:152384] button.titleLabel.origin{100, 50}
button.titleLabel.center{100, 50}
2015-09-08 13:27:43.509 IconFontTest[3458:152384] button.imageView.frame{{0, 0}, {0, 0}}

【问题讨论】:

标签: ios objective-c uiimageview uibutton frame


【解决方案1】:

视图不会在您设置它们时立即呈现。在记录其框架属性之前,您需要等待下一次绘图更新。

查看来自 Apple documentation 的 UIView 上的视图绘制周期部分。这里提到了:

对于包含使用 UIKit 或 Core Graphics 的自定义内容的视图, 系统调用视图的 drawRect: 方法。你的实施 此方法负责将视图的内容绘制到 当前图形上下文,由系统自动设置 在调用此方法之前。这将创建一个静态视觉 视图内容的表示,然后可以显示在 屏幕。

当您的视图的实际内容发生变化时,它是您的 有责任通知系统您的视图需要 重绘。您可以通过调用视图的 setNeedsDisplay 或 setNeedsDisplayInRect:视图的方法。这些方法让 系统知道它应该在下一次绘图期间更新视图 循环。 因为它要等到下一个绘图周期才能更新 视图,您可以在多个视图上调用这些方法来更新它们 同一时间。

您应该在日志方法之前调用setNeedsDisplay 方法:

[button setNeedsDisplay];

//Log methods after the above line

或者,您可以尝试在延迟(例如 0.2 秒)后调用日志方法,以等待下一个绘图周期完成。

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"button.titleLabel.frame%@",NSStringFromCGRect(button.titleLabel.frame));
        NSLog(@"button.frame%@",NSStringFromCGRect(button.frame));
        NSLog(@"button.titleLabel.origin%@\nbutton.titleLabel.center%@",NSStringFromCGPoint(button.titleLabel.frame.origin),NSStringFromCGPoint(button.titleLabel.center));
        NSLog(@"button.imageView.frame%@",NSStringFromCGRect(button.imageView.frame));
    });

【讨论】:

  • 完美,但为什么不是 imageView.frame?它仍然是零
  • Manish 的回答是正确的。只有当您使用 [button setImage]; 时才会填充 imageView 属性我相信您无法访问呈现背景图像的 UIImageView。您只能以 UIImage 的形式访问currentBackgroundImage
【解决方案2】:

button.imageView.frame 为零,因为您使用了setbackgroundImage: 方法而不是 UIButton 的setImage: 方法。

【讨论】:

  • setImage: 方法是可以的。
猜你喜欢
  • 2016-09-26
  • 1970-01-01
  • 2012-02-18
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 2011-04-11
相关资源
最近更新 更多