Xib出口视图迅速消失

问题描述 投票:0回答:1

我使用xib创建了自定义边菜单,但是如果我是第一次安装应用,则它会显示xib的子视图(在这种情况下为mainview)。但是view的IBOutlet在那里。如果我终止应用程序并再次运行它,则效果很好。它可以在模拟器中正常工作,但在实际设备中却无法正常工作。这是我的自定义类代码pls 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()
    }
}
ios swift null xib iboutlet
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)
© www.soinside.com 2019 - 2024. All rights reserved.