Swift4&xcode9.2で開発しています。
前置きが少し長いですが、サンプルコードは極力少なくします。
前置き
私は既存のUITabbarのデザインが少し気に入らないので自作しています。
後々カスタマイズなどがしやすいようにとカスタムクラスを作ったりしてそのUIと機能
(function)を分けるようにしています。
主にUIの部分をカスタムクラス&xibでつくり、画面遷移などのfunc部分をViewControllerに書いています。
UITabbarと同じものなので、遷移先のページにも同じUIが設置され、同じ機能が設置されます。
質問
UIをカスタムクラス で分離し、ViewControllerにてインスタンス生成し、表示するところまでは可能でした。
しかし、画面遷移はUIViewController内でしか行えないため、遷移用のfuncに関しては遷移前と遷移後のクラス両方に書かなくてはならないのではと考えています。
全く同じナビゲーションメニューのUIと機能を2回も書かなくてはならないことに不満を感じています。
これをなんとかまとめたいです。
そのための提案や意見をお願いします。
コードは下記に記載します。
ファイルは
FirstViewcontroller.swift
SecondViewcontroller.swift (白紙のため省略)
NavigationMenu.swift
swift
1//FirstViewController 2 3import UIKit 4 5class FirstViewController: UIViewController { 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // ナビゲーション設定 10 navigation() 11 } 12 13 override func didReceiveMemoryWarning() { 14 super.didReceiveMemoryWarning() 15 } 16 17} 18 19//問題の機能とUIのカスタマイズソース 20//この辺をSecondViewControllerにも書きそうだが、書きたくない。 21extension FirstViewController{ 22 23 func navigation() { 24 25 //navigationデザイン 26 let navigationSize = CGRect(x:0, y:screenHeight - 50, width: screenWidth, height: 50) 27 let screenSize = CGRect(x:0, y:0, width: screenWidth, height: screenHeight) 28 let bgColor = UIColor(red: 255/255,green: 255/255,blue: 255/255,alpha: 0.7) 29 let blurEffect = UIBlurEffect(style: .extraLight) 30 let visualEffectView = UIVisualEffectView(effect: blurEffect) 31 let bgView = UIView(frame: screenSize) 32 let commonNavigation = NavigationMenu.instance() 33 bgView.backgroundColor = bgColor 34 visualEffectView.frame = navigationSize 35 commonNavigation.frame = navigationSize 36 self.view.addSubview(visualEffectView) 37 self.view.addSubview(bgView) 38 self.view.addSubview(commonNavigation) 39 commonNavigation.iconSetting() 40 41 //navigationアクション 42 let sWHalf = screenWidth / 2 43 let iconAFrame = CGRect(x:0, y:screenHeight - 50, width: sWHalf, height: 50) 44 let iconBFrame = CGRect(x:sWHalf, y:screenHeight - 50, width: sWHalf, height: 50) 45 let iconAAction = UIButton() 46 let iconBAction = UIButton() 47 iconAAction.frame = iconAFrame 48 iconBAction.frame = iconBFrame 49 iconAAction.addTarget(self, 50 action: #selector(self.iconATapped(sender:)), 51 for: .touchUpInside) 52 iconBAction.addTarget(self, 53 action: #selector(self.iconBTapped(sender:)), 54 for: .touchUpInside) 55 self.view.addSubview(iconAAction) 56 self.view.addSubview(iconBAction) 57 } 58 59 @objc func iconATapped(sender : AnyObject) { 60 //ここにFirstViewcontrollerへの画面遷移を書く 61 } 62 63 @objc func iconBTapped(sender : AnyObject) { 64 //ここにSecondViewcontrollerへの画面遷移を書く 65 } 66}
swift
1//Storyboardと共に作られたナビゲーション用のコード 2 3import UIKit 4 5class NavigationMenu: UIView { 6 let firstViewController = FirstViewController() 7 8 @IBOutlet weak var navigationMenu: UIView! 9 @IBOutlet weak var iconAButton: UILabel! 10 @IBOutlet weak var iconBButton: UILabel! 11 @IBOutlet weak var iconALabel: UILabel! 12 @IBOutlet weak var iconBLabel: UILabel! 13 14 var iconA:String! = "?" 15 var iconB:String! = "?" 16 var textA:String! = "TESTA" 17 var textB:String! = "TESTB" 18 19 class func instance() -> NavigationMenu { 20 return UINib(nibName: "NavigationMenu", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! NavigationMenu 21 } 22 23 func iconSetting(){ 24 iconAButton.text = iconA 25 iconBButton.text = iconB 26 iconALabel.text = textA 27 iconBLabel.text = textB 28 } 29 30}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/17 04:28 編集