• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何在 UITabBarController 中的所有选项卡上制作 UIView

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:17:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在 uitabbar Controller 的所有选项卡之上制作 uiview。 enter image description here我计划制作一个名为“uiView.swift”的baseviewcontroller。在“uiView.swift”中,我添加了我的uiview。之后,我想从该 uiView.swift 继承每个选项卡 ViewController(比如“resturent.swift”)。因此,在 UITabBarController 的每个选项卡中,您将看到“uiView.swift” View resued.uiView.swift 连接到 Storyboard 中的 ViewController,该按钮具有在按钮单击时显示 View 的按钮。这是我的“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)

    }


}

如何在标签栏 Controller 的每个选项卡中重用“uiView.swift”中的 View (即“resturent.swif”)

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 更改为登录 View Controller (Viewcontroller.swift)以运行应用程序。我如何通过继承重用uiView.swift 中的 View ?您可以从此链接下载项目。 https://drive.google.com/file/d/1XSwOZcfvglB_7Zt_E8W8W3Dym3i1_lrB/view?usp=sharing



Best Answer-推荐答案


您不应在任何 View 上添加菜单 View 。取而代之的是,您可以直接从 AppDelegate 将其添加到您的窗口中。 在窗口上添加 View 会将其置于当前可见的所有 View 之上。

关于ios - 如何在 UITabBarController 中的所有选项卡上制作 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300761/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap