add correct code
[app.GameProjectTest] / GameProjectTest / HomeController.swift
1 //
2 //  HomeViewController.swift
3 //  UPlab_MVC
4 //
5 //  Created by Ernest.lee on 17/10/2019.
6 //  Copyright © 2019 Ernest.lee. All rights reserved.
7 //
8
9 import UIKit
10 import SceneKit
11        
12  
13 public extension UIView {
14     enum ViewSide {
15         case Left, Right, Top, Bottom
16     }
17     func addBorder(toSide side: ViewSide, withColor color: CGColor, andThickness thickness: CGFloat) {
18
19         let border = CALayer()
20         border.backgroundColor = color
21
22         switch side {
23         case .Left: border.frame = CGRect(x: frame.minX, y: frame.minY, width: thickness, height: frame.height); break
24         case .Right: border.frame = CGRect(x: frame.maxX, y: frame.minY, width: thickness, height: frame.height); break
25         case .Top: border.frame = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: thickness); break
26         case .Bottom: border.frame = CGRect(x: frame.minX, y: frame.maxY, width: frame.width, height: thickness); break
27         }
28
29         layer.addSublayer(border)
30     }
31     func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?,
32                 paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat,
33                 width: CGFloat, height: CGFloat, enableInsets: Bool) {
34         var topInset = CGFloat(0)
35         var bottomInset = CGFloat(0)
36         
37         if #available(iOS 11, *), enableInsets {
38             let insets = self.safeAreaInsets
39             topInset = insets.top
40             bottomInset = insets.bottom
41             
42         }
43         
44         
45         translatesAutoresizingMaskIntoConstraints = false
46         
47         if let top = top {
48             self.topAnchor.constraint(equalTo: top, constant: paddingTop+topInset).isActive = true
49         }
50         if let left = left {
51             self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
52         }
53         if let right = right {
54             rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
55         }
56         if let bottom = bottom {
57             bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom-bottomInset).isActive = true
58         }
59         if height != 0 {
60             heightAnchor.constraint(equalToConstant: height).isActive = true
61         }
62         if width != 0 {
63             widthAnchor.constraint(equalToConstant: width).isActive = true
64         }
65         
66     }
67 }
68 /*
69  HomeVC is the main controller, it is responsible for Nav bar control and setting up childVC
70  */
71
72 class HomeController: UIViewController{
73     
74    
75   
76     //MARK: Properties
77     let mapVC: GameViewController = GameViewController()
78     
79     
80      
81     //MARK: Init
82     override func viewDidLoad() {
83         super.viewDidLoad()
84         view.backgroundColor = .gray
85         setUpVC()
86         setUpNavBar()
87         setConstraints()
88     }
89     
90     
91     
92     func setUpVC(){
93        
94         //Map VC
95         
96         addChild(mapVC)
97
98       //  if let frame = nil {
99       //      mapVC.view.frame = frame
100       //  }
101
102         view.addSubview(mapVC.view)
103         mapVC.didMove(toParent: self)
104       
105         
106     }
107     
108     
109     func setConstraints(){
110         /*
111         mapVC.view.anchor(top: nil, left: nil,
112                           bottom: nil, right: nil,
113                           paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0, enableInsets: false)
114         if #available(iOS 11.0, *) {
115             mapVC.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
116         }
117         
118         mapVC.view.addBorder(toSide: .Left, withColor: UIColor.black.cgColor, andThickness: 1.0)
119         */
120        
121         
122     }
123 }
124
125
126
127 extension HomeController {
128     func setUpNavBar(){
129         navigationController?.navigationBar.barTintColor = .white
130         navigationController?.navigationBar.isTranslucent = true
131         navigationController?.navigationBar.backgroundColor = .white
132         
133        
134         let threeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "Image").withRenderingMode(.alwaysOriginal),
135                                           style: .plain,
136                                           target: self,
137                                           action: #selector(handleThree))
138         
139
140         
141         
142         navigationItem.leftBarButtonItems = [threeButton]
143          
144     }
145     
146     //MARK: - Nav Bar Handler
147     
148     
149     @objc func handleThree(){
150         //delegate?.handleThreeToggle()
151         
152     }
153    
154 }
155