代码之家  ›  专栏  ›  技术社区  ›  Jake2Finn

Swift Access帮助程序功能视图

  •  0
  • Jake2Finn  · 技术社区  · 6 年前

    我尝试从帮助程序类访问函数“addNavBar()”,但是当我运行模拟器时,homeViewController上不显示任何视图。

    斯威夫特4

    主页视图控制器.swift

    class HomeController: UIViewController {
    let NavBar = NavigationBarHelper()
    override func viewDidLoad() {
        super.viewDidLoad()
        NavBar.addNavBar()
    }
    

    }

    导航栏帮助程序.swift

    class NavigationBarHelper: UIView {
        func addNavBar() {
            let rect = CGRect(x: 10, y: 70, width: 250, height: 100)
            let navBarView = UIView(frame: rect)
            navBarView.backgroundColor = UIColor.blue
            self.addSubview(navBarView)
        }
    }
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   vikingosegundo    6 年前

    self

    class NavigationBarHelper {
        func addNavBar(to view: UIView) {
            let rect = CGRect(x: 10, y: 70, width: 250, height: 100)
            let navBarView = UIView(frame: rect)
            navBarView.backgroundColor = UIColor.blue
            view.addSubview(navBarView)
        }
    }
    

    class HomeController: UIViewController {
    let navBarHelper = NavigationBarHelper()
    override func viewDidLoad() {
        super.viewDidLoad()
        navBarHelper.addNavBar(to: self.view)
    }
    
        2
  •  0
  •   Shehata Gamal    6 年前

    let NavBar = NavigationBarHelper()
    

    static

    class NavigationBarHelper: UIView {
     static func addNavBar(view:UIView) {
        let rect = CGRect(x: 10, y: 70, width: 250, height: 100)
        let navBarView = UIView(frame: rect)
        navBarView.backgroundColor = UIColor.blue
        view.addSubview(navBarView)
     }
    }
    

    NavigationBarHelper.addNavBar(view: self.view)
    
        3
  •  -1
  •   hardikdevios    6 年前

    extension

    extension UIView {
        func addNavBar() {
            let rect = CGRect(x: 10, y: 70, width: 250, height: 100)
            let navBarView = UIView(frame: rect)
            navBarView.backgroundColor = UIColor.blue
            self.addSubview(navBarView)
        }
    }
    

    UIViewController

    self.view.addNavBar()