我使用 xib 创建了自定义侧边菜单,但是如果我第一次安装应用程序,它会显示 xib 的子视图(在这种情况下是主视图)为零。但是视图的 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()
}
}