简体   繁体   English

自定义UIView nativeContentSize宽度变得非常大或为零

[英]Custom UIView intrinsicContentSize width gets ridiculously large values or zero

I am trying to make an offset-ed background to a UILabel in Xcode 9.1. 我正在尝试为Xcode 9.1中的UILabel创建偏移量背景。

I have a @IBDesignable subclass of UIView which contains UILabel. 我有一个包含UILabel的UIView的@IBDesignable子类。 I have next constraints: 我还有下一个约束:

  1. label.centerX == self.centerX label.centerX == self.centerX
  2. label.centerY == self.centerY label.centerY == self.centerY
  3. width: self == 1 x label + offset; 宽度:自我== 1 x标签+偏移量; (offset usually - 20) (通常偏移-20)
  4. height: self == mult x label + 0; 身高:自我==多人x标签+ 0; (mult usually - 1.3) (通常为1.3)

I have also overridden intrinsicContentSize : 我也重写了intrinsicContentSize

override var intrinsicContentSize: CGSize {
    get {
        var size = self.label.intrinsicContentSize
        size.width += offset
        size.height *= self.mult
        return size
    }
}

Now sometimes both if I debug the code and stop on a breakpoint in my overridden intrinsicContentSize but mostly in Interface Builder I get some ridiculously large values for width like 20,000,010 or something similar. 现在有时候,如果我调试代码并在覆盖的intrinsicContentSize的断点处停止,但是大多数情况下,在Interface Builder中,我会得到一些可笑的大宽度值,例如20,000,010或类似的值。 They are pretty random but quite large. 它们是随机的,但很大。 Last time Interface Builder complained that you can only make 10,000x10,000 UIView. 上次Interface Builder抱怨您只能制作10,000x10,000 UIView。

大价值截图

Interface Builder投诉

In other cases the view intrinsicContentSize.width is 0 and setting placeholder for intrinsicContentSize for my view doesn't help. 在其他情况下,视图internalContentSize.width为0,并且为我的视图设置internalContentSize的占位符无济于事。

If you go to other swift file and come back it can change. 如果您转到其他快速文件,然后回来,它可能会更改。 Sometimes it show correct value but in most of the times something bogus. 有时它显示正确的值,但在大多数情况下是虚假的。

The final layout usually is what I intended, so it works in runtime but I pretty much can't use Interface Builder with this view. 最终的布局通常是我想要的,因此它可以在运行时使用,但是我几乎不能在此视图中使用Interface Builder。

I also tried to init UILabel with empty text or one char text like '.' 我还尝试使用空文本或一个字符字符(如“。”)初始化UILabel。 and frame (0,0,1,1) as I thought that maybe zero frame and nil text provides this behaviour but it seems that doesn't help either. 和frame(0,0,1,1),因为我认为零帧和nil文本可能会提供这种行为,但似乎也无济于事。

Do you have any ideas how I can fix this so I can use my OffsetLabel in Interface Builder? 您对如何解决此问题有任何想法,以便可以在Interface Builder中使用我的OffsetLabel吗? Why does this happen? 为什么会这样?

似乎重新启动Xcode后,它会按预期工作一段时间。

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

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