简体   繁体   English

使用Swift在Xcode 9中不显示IBDesignable UI

[英]IBDesignable UI is not showing in Xcode 9 with Swift

I trying to using my custom xib files. 我试图使用我的自定义xib文件。 But those UI are not showing in main view controller in xcode. 但是那些UI没有在xcode中的主视图控制器中显示。 But it show when run the app. 但它显示运行应用程序时。 I already added @IBDesignable in class. 我已经在课堂上添加了@IBDesignable。 Here is what i did. 这就是我做的。

  1. I created TestView.xib and added some design 我创建了TestView.xib并添加了一些设计

  2. I created TestView.swift 我创建了TestView.swift

  3. I updated fileOwner of TestView.xib with TestView in Custom Class 我在Custom Class中使用TestView更新了TestView.xib的fileOwner

  4. I added init code to testView.swift 我向testView.swift添加了init代码

Code inside TestView.swift TestView.swift中的代码

import UIKit

@IBDesignable class DetailView: UIView {

override init(frame: CGRect){
    super.init(frame: frame)
    self.setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    self.setup()

}

func setup() {
    let view = Bundle.main.loadNibNamed("TestView", owner: self, options: nil)?.first as! UIView
    view.frame = self.bounds
    self.addSubview(view)
}

}
  1. And Added on UIView in MainViewController and link to TestView with custom class name. 并在MainViewController中的UIView上添加,并使用自定义类名链接到TestView。

  2. After that i run the app, I can see the UI from testView. 之后我运行应用程序,我可以从testView中看到UI。 Image of emulator and result 模拟器和结果的图像

  3. But my custom UI don't show in MainViewController in xcode Image of xcode view controller 但我的自定义UI不会在xcode视图控制器的 xcode 图像中的MainViewController中显示

How can i enable to see my custom UI in MainViewController ? 如何在MainViewController中启用自定义UI? Thanks 谢谢

In InterfaceBuilder select your ViewController and choose "Editor-->Refresh all Views". 在InterfaceBuilder中,选择ViewController并选择“Editor - > Refresh all Views”。 Then your custom view should come up. 然后你的自定义视图应该出现。

If something is wrong you can debug your IBDesignable class by setting breakpoints in your code and then choose "Editor-->Debug selected views". 如果出现问题,您可以通过在代码中设置断点来调试IBDesignable类,然后选择“编辑器 - >调试所选视图”。

prepareForInterfaceBuilder() only needs to be implemented if you need special design appearance in InterfaceBuilder. 如果在InterfaceBuilder中需要特殊的设计外观,则只需要实现prepareForInterfaceBuilder()

Sometimes it is necessary to clean the project and build folder to get it to work: 有时需要清理项目并构建文件夹以使其工作:
Project->Clean 项目 - >清除
Project->(hold down options key)->Clean Build Folder 项目 - >(按住选项键) - >清理构建文件夹

I just changed the setup function. 我刚刚更改了设置功能。 It working now. 它现在正在运作。 Final Result Image - It showing UI in IB 最终结果图像 - 它在IB中显示UI

func setup() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleHeight.rawValue)))

    self.addSubview(view)
}

func loadViewFromNib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "TestView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
}

Override two func below for live: 覆盖以下两个func for live:

override open func prepareForInterfaceBuilder() {
   layer.cornerRadius = cornerRadius
   layer.borderWidth = borderWidth
}

override open func awakeFromNib() {
   layer.cornerRadius = cornerRadius
   layer.borderWidth = borderWidth
}

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

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