【问题标题】:Custom UIView does not fit in bounds of screen , Swift自定义 UIView 不适合屏幕,Swift
【发布时间】:2016-01-31 21:20:32
【问题描述】:

我有一个从 xib 文件加载的自定义 UIView,当在模拟器中运行时,视图似乎被剪裁在右侧。我试图更改“view.autoresizingmask”中的属性,但是使用不同的可用选项并没有帮助,使用不同的约束选项也不能解决问题,

我在自定义视图类的 swift 文件中使用的代码如下:

  var view : UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    loadViewFromNib ()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    loadViewFromNib ()
}
func loadViewFromNib() {
    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "LeaderBoardView", bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    view.frame = bounds
    view.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
    self.addSubview(view);

}

This is an image of the xib file ( Keep in mind the view that this xib is loaded into are the same dimensions as the file )

This image comes from the simulator and here you see how the view will not fit in the bounds of the screen.

SB:还有一个轻微的渲染问题,也许有代码可以帮助解决这个问题。错误信息如下 “无法渲染 LeaderBoardView 的实例:渲染视图花费的时间超过 200 毫秒。您的绘图代码可能会遇到性能下降的问题。”

【问题讨论】:

    标签: ios xcode swift uiview xib


    【解决方案1】:

    不要再次在loadViewFromNib 中创建视图,你已经在上面添加了,只是:

    view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    

    bundle 可以是nil

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-07
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      相关资源
      最近更新 更多