简体   繁体   English

当UIView的尺寸类应用,对于一个UIView直接从XIB装入?

[英]When are UIView size classes applied, for a UIView loaded directly from a Xib?

I'm initting a UIView with a xib, programatically: 我正在以编程方式初始化带有xib的UIView:

- (id)initWithFrame:(CGRect)frame
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"MyViewNib" owner:self options:nil] objectAtIndex:0];
    if (self)
    {
        //perform setup of various components

        return self;
    }
}

This view uses size classes for Any Width, Any Height, and Any Width, Compact Height. 此视图将尺寸类用于“任意宽度”,“任何高度”和“任意宽度”,“紧凑高度”。

I have a UIButton in a xib that I need to put a circle in the background of, like so: 我在xib中有一个UIButton,需要在其背景中放一个圆圈,如下所示:

self.closeButton.layer.cornerRadius = self.closeButton.frame.size.width / 2.0f;
self.closeButton.layer.backgroundColor = [UIColor colorWithWhite:164.0f / 255.0f alpha:1.0f].CGColor;

If I try setting the button's corner radius in the init function when I'm on a device using Any Width, Compact Height, the label's frame is still set to the Any Width, Any Height value. 如果我在使用“任意宽度,紧凑高度”的设备上尝试在init函数中设置按钮的拐角半径,则标签的框架仍设置为“任意宽度,任意高度”值。 I've also tried overriding layoutSubviews and setting the value there, with no luck. 我也尝试覆盖layoutSubviews并在那里设置值,但是没有运气。 It appears that the size class constraints are applied after layoutSubviews without another call to layoutSubviews, since the other components appear on-screen correctly. 似乎在classLayoutSubviews之后应用了大小类约束,而无需再次调用layoutSubviews,因为其他组件可以正确显示在屏幕上。

So, I'm wondering if there's a good entry point for me to catch where the size classes are applied, so that I can set the background corner radius of the button correctly. 因此,我想知道是否有一个很好的入口点可以让我了解应用大小类的位置,以便可以正确设置按钮的背景角半径。 I could just set the button up programmatically, but I'd like to figure out how to do this since it will probably come up again in the process of converting to size classes. 我可以通过编程方式设置按钮,但是我想弄清楚如何执行此操作,因为在转换为尺寸类的过程中它可能会再次出现。

在您的xib类中-(void)awakeFromNib;

This works if I put it at the end of the init function: 如果我将它放在init函数的末尾,这将起作用:

 [self performSelector:@selector(updateButtonBackground) withObject:nil afterDelay:.001];

Apparently by this point the constraints have been set up with the size classes. 显然,到此为止,已经使用size类设置了约束。 It's certainly non-ideal though, so if anyone has an answer that's event-driven or involves view "lifecycle" (as far as that applies to UIView) I'll accept that... 但是,这肯定不是理想的,因此,如果有人得到事件驱动的答案或涉及视图“生命周期”(就适用于UIView的观点),我将接受...

How about doing this? 怎么样呢?

-(void)layoutSubviews
{
    [super layoutSubviews];
self.closeButton.layer.cornerRadius = self.closeButton.frame.size.width / 2.0f;
self.closeButton.layer.backgroundColor = [UIColor colorWithWhite:164.0f / 255.0f alpha:1.0f].CGColor;
//more of your changes
[self layoutIfNeeded]; // You can remove this line if it works without it for you..
}

I know this thread is old, but I see no good answer here. 我知道这个线程很旧,但是我在这里看不到任何好的答案。

Size classes are defined in traitCollectionDidChange: method. 大小类在traitCollectionDidChange:方法中定义。 You can't ensure that traitCollection is set in initWithFrame: or awakeFromNib: . 您不能确保在initWithFrame:awakeFromNib:设置traitCollection。 So set your corner radius in traitCollectionDidChange: method based on the view size classes. 因此,根据视图大小类在traitCollectionDidChange:方法中设置拐角半径。

You need to apply the corner radius in viewDidLayoutSubviews . 您需要在viewDidLayoutSubviews应用拐角半径。 It is at this point that you know the bounds of the button have been established. 至此,您知道按钮的边界已建立。

You can see an example on my blog . 您可以在我的博客上看到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM