2017-11-15 10 views
1

uitabbarコントローラのすべてのタブの上にuiviewを作成するには。 enter image description here私は "uiView.swift"という名前のbaseviewcontrollerを作るつもりでした。 "uiView.swift"でuiviewを追加しました。その後、私は各タブViewController( "resturent.swift"と言う)をそのuiView.swiftから継承したいと思う。したがって、UITabBarControllerの各タブには、 "uiView.swift"ビューresued.uiView.swiftがストーリーボードのViewControllerに接続されています。このビューにはボタンクリック時のビューを表示するボタンがあります。これは私の "uiView.swift"ですUITabBarController内のすべてのタブの上にUIViewを作成する方法

私はタブバーコントローラ(すなわち「resturent.swif」)の各タブで「uiView.swift」からビューの可能な再利用を行うことができますどのように
class uiView: UIViewController { 
var menuView: UIView? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     menuView = UIView(frame: CGRect(x: 0, y: -200, width: 420, height: 200)) 
     menuView?.backgroundColor = UIColor.green 
     view.addSubview(menuView!) 
    } 
    @objc func MyBag(){ 

    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 
    @IBAction func collectionmenuone(_ sender: Any) { 
     menuView=UIView(frame: CGRect(x: 0, y: 0, width: 420, height: 200)) 
     menuView?.backgroundColor=UIColor.lightGray 
     self.view.addSubview(menuView!) 

     var btnbag = UIButton(type: .roundedRect) 
     btnbag.addTarget(self, action: #selector(self.MyBag), for: .touchUpInside) 
     btnbag.frame = CGRect(x: 104, y:130 , width: 150, height: 60) 
     btnbag.setTitle("Done", for: .normal) 
     btnbag.backgroundColor=UIColor.green 
     menuView?.addSubview(btnbag) 

    } 


} 

class resturent:UICollectionViewController,UICollectionViewDelegateFlowLayout { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     navigationItem.title="Seafood" 
     collectionView?.backgroundColor=UIColor.white 
     // view.backgroundColor=UIColor.redColor 
     collectionView?.register(VideoCell.self, forCellWithReuseIdentifier: "cellid") 
    } 
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 
    } 
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) 
     // cell.backgroundColor=UIColor.red 
     return cell 
    } 
    /* override func collectionView(collectionView: UICollectionView, cellForItemAtIndexpath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath as IndexPath) 

    return cell 
    }*/ 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let height=(view.frame.width - 16 - 16) * 9/16 
     return CGSize(width: view.frame.width, height: height + 16 + 68) 
    } 
} 
class VideoCell:UICollectionViewCell{ 
    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setupView() 
    } 
    let thumbnailImageView:UIImageView = { 
     let imageView=UIImageView() 
     imageView.backgroundColor=UIColor.blue 
     imageView.image=UIImage(named: "food24") 
     imageView.contentMode = .scaleAspectFill 
     imageView.clipsToBounds=true 
     return imageView 
    }() 
    let userProfileImageView:UIImageView={ 
     let imageView=UIImageView() 
     //imageView.backgroundColor=UIColor.green 
     return imageView 
    }() 
    let separatorView:UIView = { 
     let view = UIView() 
     view.backgroundColor=UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1) 
     return view 
    }() 
    let titleLabel:UILabel={ 
     let label = UILabel() 
     label.translatesAutoresizingMaskIntoConstraints=false 
     label.text="Resturant name here" 
     return label 
    }() 
    let subtitleTextView:UITextView = { 
     let textView=UITextView() 
     textView.translatesAutoresizingMaskIntoConstraints=false 
     textView.textContainerInset=UIEdgeInsetsMake(0, -4, 0, 0) 
     textView.textColor=UIColor.lightGray 
     textView.text = "SeaFood" 
     return textView 
    }() 
    func setupView() { 
     //backgroundColor=UIColor.blue 
     addSubview(thumbnailImageView) 
     addSubview(separatorView) 
     addSubview(userProfileImageView) 
     addSubview(titleLabel) 
     addSubview(subtitleTextView) 
     addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", view: thumbnailImageView) 
     addConstraintsWithFormat(format: "H:|-16-[v0(44)]", view: userProfileImageView) 
     //vertial constratints 
     addConstraintsWithFormat(format: "V:|-16-[v0]-8-[v1(44)]-16-[v2(1)]|", view: thumbnailImageView,userProfileImageView,separatorView) 
     addConstraintsWithFormat(format: "H:|[v0]|", view: separatorView) 
     //top constraints 
     addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .top, relatedBy: .equal, toItem:thumbnailImageView , attribute: .bottom, multiplier: 1, constant: 8)) 
     //left constaints 
     addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .left, relatedBy: .equal, toItem: userProfileImageView, attribute: .right, multiplier: 1, constant: 8)) 
     //right constraint 
     addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .right, relatedBy: .equal, toItem: thumbnailImageView, attribute: .right, multiplier: 8, constant: 0)) 
     //height constraint 
     addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 20)) 

     //top constraints 
     addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .top, relatedBy: .equal, toItem:titleLabel , attribute: .bottom, multiplier: 1, constant: 4)) 
     //left constaints 
     addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .left, relatedBy: .equal, toItem: userProfileImageView, attribute: .right, multiplier: 1, constant: 8)) 
     //right constraint 
     addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .right, relatedBy: .equal, toItem: thumbnailImageView, attribute: .right, multiplier: 8, constant: 0)) 
     //height constraint 
     addConstraint(NSLayoutConstraint(item: subtitleTextView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 20)) 


     // addConstraintsWithFormat(format: "V:[v0(20)]", view: titleLabel) 
     // addConstraintsWithFormat(format: "H:|[v0]|", view: titleLabel) 

    } 
    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 
extension UIView{ 
    func addConstraintsWithFormat(format:String,view:UIView...){ 
     var viewDictionary=[String:UIView]() 
     for (index,view) in view.enumerated(){ 
      let key="v\(index)" 
      view.translatesAutoresizingMaskIntoConstraints=false 
      viewDictionary[key]=view 
     } 
     NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: format , options: NSLayoutFormatOptions(), metrics: nil, views: viewDictionary)) 
    } 

} 

uiView.swiftは、タイプのUIViewControllerとresturent.swifでありますタイプはUICollectionViewControllerです.uiView.swiftからappへのエントリーポイントを変更してログインすることができます。View Controller(Viewcontroller.swift)を使用してapp.Howを継承して再利用できますuiView.swiftのビュー?このリンクからプロジェクトをダウンロードできます。 https://drive.google.com/file/d/1XSwOZcfvglB_7Zt_E8W8W3Dym3i1_lrB/view?usp=sharing

+0

tabBarControllerでpopUpを作成していて、現在表示されているすべてのビューの上にこのpopUpを表示しますか?右? –

+0

tabBarController上のpopUpボタンをクリックする –

+0

そのボタンをクリックして現在何をしているのかを強調できますか? uiView.swift上の –

答えて

0

ビューのいずれかにメニュービューを追加しないでください。この代わりに、AppDelegateからウィンドウに直接追加することができます。 ウィンドウにビューを追加すると、現在表示されているすべてのビューの上にビューが配置されます。

+0

uiView.swiftからログインビューコントローラ(Viewcontroller)へのアプリケーションのエントリポイントを変更できます。迅速に)アプリを実行します。 –

+0

私はすべてのタブバーコントローラでuiviewを使いたいと思っています。すべてのタブバーコントローラでuiviewを持っていると思う方法は、uiView.Swiftからタブバーコントローラのすべてのタブにビューを継承することです。 ? –

+0

なぜすべてのView Controller上のものに表示したいのですか? あなたは上にすべてを表示することができます。 –

関連する問題