自定义UIView不适合屏幕边界,Swift

时间:2016-01-31 21:20:33

标签: ios xcode swift uiview xib

我有一个自定义UIView,我从xib文件加载,当在模拟器中运行时,视图似乎被剪切在右侧。我试图在" 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毫秒。您的绘图代码可能会遇到性能下降的问题。"

1 个答案:

答案 0 :(得分:0)

不要再次在loadViewFromNib创建视图,您已经添加到最顶层,只是:

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

bundle可以是nil