UIView在没有UIViewController的情况下获得宽度/高度

时间:2017-07-05 23:28:39

标签: ios swift uiview

我有这个:

class SheetView: UIView {

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

    RCTLogInfo("SHEET VIEW INIT'ED")
    let width = String(format:"%.3f", Double(self.frame.size.width));
    let height = String(format:"%.3f", Double(self.frame.size.height));
    RCTLogInfo("Width: " + width + ", Height: " + height);

  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

然而,宽度和高度始终为0.在这里阅读Access UIView width at runtime我似乎需要viewDidLayoutSubviews,但UIView似乎没有这个。我必须使用UIViewController来使用viewDidLayoutSubviews,但我无法从UIView更改,因为此组件的使用位置需要UIView

class SheetViewManager : RCTViewManager {

  override func view() -> UIView! {
    return SheetView();
  }

}

无论如何只能在UIView

中获得高度/宽度

1 个答案:

答案 0 :(得分:1)

是的,你确定可以。试试这个 。 (Swift 3.0)

override func layoutSubviews() {
   super.layoutSubviews()
  print(self.frame)
}

请确保您知道,如果您的自定义视图的超级视图未通过框架设置或通过故事板或通过编程方式自动布局为您的自定义视图提供框架,则您的框架将保持0宽度和0高度

相关问题