我有一个Xib文件试图用我的故事板进行设置。 Xib文件中的所有内容都很好,但由于某种原因,它没有显示。我从GitHub导入了一个文件,它被设置为我的Xib,它在Objective-C中,我设置了桥接,没有错误。但是,当我运行它时,没有任何显示它的空白我没有在View Controller中设置一些东西吗?一切都是以编程方式完成的,我只是在故事板中设置了类。
故事板的屏幕截图:
当我推送到ViewController时,模拟器给了我什么:
这是我应该看到的:
我想要实现的 - https://github.com/jberlana/JBCroppableView
我的XIB课程
import UIKit
class CropViewXIB: UIView {
@IBOutlet weak var ImageView: JBCroppableImageView!
@IBAction func SubAction(_ sender: Any) {
ImageView.removePoint()
}
@IBAction func AddAction(_ sender: Any) {
ImageView.addPoint()
}
@IBAction func UndoAction(_ sender: Any) {
ImageView.reverseCrop()
}
@IBAction func CropAction(_ sender: Any) {
ImageView.crop()
}
override init(frame: CGRect) {
super.init(frame: frame)
commomInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commomInit()
}
private func commomInit(){
Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)
self.addSubview(ImageView)
ImageView.frame = self.bounds
ImageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}
我的视图控制器
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var cropView: CropViewXIB!
override func viewDidLoad() {
super.viewDidLoad()
}
}
尝试使用编程而不是使用storyboard加载xib。
override func viewDidLoad()
{
super.viewDidLoad()
guard let yourXIB = Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)?.first as? CropViewXIB else { return}
self.view.addSubview(yourXIB)
}
问题是您实际上没有获得UINib对象的父视图。
Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)
上面的行返回一个[Any]
,在你的情况下你甚至没有使用它返回的视图。所以我的想法是从它获取第一个对象并将其转换为UIView
,例如:
Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)?.first as? UIView
就个人而言,这就是我与笔尖互动的方式。我创建了一个view
类型的UIView
属性,它可以被称为nib的父视图,并且所有子视图都被添加到它而不是self
。
像这样的东西:
final class SomeNibView: UIView {
public var view: UIView!
private func setup() { // called to the initializer
// grab the views from loadNibNamed
guard let _view = Bundle.main.loadNibNamed("name", owner: self, options: nil)?.first as? UIView else { return }
// set it to our view property
view = _view
// add this property to the nib subview aka self
addSubview(view)
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
private func addMulitpleSubviews() {
// instead of doing self.addSubview(....) when it comes to add other subviews
// you'll do this view.addSubview(....)
}
}