Xib出口视图变得零迅速



我使用 xib 创建自定义侧菜单,但如果我第一次安装应用程序,它会显示 xib 的子视图(在本例中为主视图(nil。 但View的IBOutlet就在那里。如果我终止应用程序并再次运行它,它可以正常工作。 它在模拟器中工作正常,但在真实设备中无法正常工作。 这是我的自定义类代码请CHK

class customSideMenu: UIView {
static let instance = customSideMenu()
weak var delegate:sideMenuDelegate?
static var dataSource:[String]?
@IBOutlet weak var tblLeftSpace: NSLayoutConstraint!
@IBOutlet weak var mainView: UIView!
@IBOutlet weak var tblView: UITableView!
@IBOutlet weak var tblWidth: NSLayoutConstraint!
var dicuserdetails = NSDictionary()
let userLoginDetails = modelClass.userLoginDetails
override init(frame: CGRect) {
super.init(frame: frame)
Bundle.main.loadNibNamed("customSideMenu", owner: self, options: nil)
tblView.delegate = self
tblView.dataSource = self
tblView.layer.cornerRadius = 10.0
tblView.clipsToBounds = true
tblView.tableFooterView = UIView()
tblView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
//tblLeftSpace.constant = -350
tblView.backgroundColor = UIColor(red:0.98, green:0.98, blue:0.98, alpha:1.0)
}
override func awakeFromNib() {
super.awakeFromNib()
}


required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: Show
func show(view:UIView){
if self.mainView == nil {
print(">>>>>>>>>>>>>>>>>>>>>main view re-init>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
// self.show(view: view)
return
}
self.mainView.alpha = 1.0
self.mainView.frame = view.bounds
if UIDevice.current.userInterfaceIdiom == .pad {
self.tblLeftSpace.constant = -350
} else {
self.tblLeftSpace.constant = -820
}
view.addSubview(self.mainView)
let top = NSLayoutConstraint(item: mainView!, attribute: .top, relatedBy: .equal,
toItem: view, attribute: .top,
multiplier: 1.0, constant: 0.0)
let bottom = NSLayoutConstraint(item: mainView!, attribute: .bottom, relatedBy: .equal,
toItem: view, attribute: .bottom,
multiplier: 1.0, constant: 0.0)
let leading = NSLayoutConstraint(item: mainView!, attribute: .leading, relatedBy: .equal,
toItem: view, attribute: .leading,
multiplier: 1.0, constant: 0.0)
let trailing = NSLayoutConstraint(item: mainView!, attribute: .trailing, relatedBy: .equal,
toItem: view, attribute: .trailing,
multiplier: 1.0, constant: 0.0)
view.addConstraints([top, bottom, leading, trailing])
UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.2, options: .curveEaseIn, animations: {
self.tblLeftSpace.constant = 0
view.layoutIfNeeded()
}, completion: nil)
tblView.reloadData()
}
}

嘿,我已经为这种用法做了一个扩展,你可以使用它

extension UIView {
public func loadViewForClass(_ className: AnyClass, fromXib xibName: String) {
guard let xibViews : [UIView] = Bundle(for: className)
.loadNibNamed(xibName, owner: self, options: nil)
as? [UIView] else {
return
}
guard let contentView = xibViews.first else {
return
}
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}

您可以将其用作

class CustomSideMenu: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
loadViewForClass(CustomSideMenu.self, fromXib: "CustomSideMenu")
}

}

并根据需要初始化它:P希望对您有所帮助。

同样在您的情况下,您正在初始化

static let instance = customSideMenu() with no frames 

此外,您正在通过捆绑包加载视图,而不是将其添加到任何地方

Bundle.main.loadNibNamed("customSideMenu", owner: self, options: nil)

最新更新