0

我使用 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()
    }
}
4

1 回答 1

0

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

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)
于 2019-12-11T07:37:52.160 回答