【问题标题】:How to add a static bottom view in swift如何在swift中添加静态底部视图
【发布时间】:2016-05-20 11:05:25
【问题描述】:

我想在我的UIViewController 底部添加一个静态UIView。这是一个用 swift 编写的故事板应用程序。在我的viewDidLoad() 中,我做了这样的事情。

self.vWindow = UIApplication.sharedApplication().keyWindow
let mainBound = CGRectMake(0, self.view.frame.size.height-135, self.view.frame.size.width, 135)//CGSizeMake(UIScreen.mainScreen().bounds.size.width, 135.0)
self.vwVideo = UIView.init(frame: mainBound)
self.vwVideo.backgroundColor = UIColor.blackColor()
self.vWindow.addSubview(self.vwVideo)

但这对我的viewController 没有任何帮助。这是为什么?请帮我。 谢谢

【问题讨论】:

  • 你可以像这样添加它:self.view.addSubView(self.vwVideo)

标签: ios swift uiview uiviewcontroller


【解决方案1】:

let vWindow = UIApplication.sharedApplication().keyWindow
let mainBound = CGRectMake(0, self.view.frame.size.height-135, self.view.frame.size.width, 135)//CGSizeMake(UIScreen.mainScreen().bounds.size.width, 135.0)
self.vwVideo = UIView.init(frame: mainBound)
self.vwVideo.backgroundColor = UIColor.blackColor()
vWindow!.addSubview(self.vwVideo)
  • 使用此代码,它将在底部的窗口中添加您的视图。

【讨论】:

  • 与用户的问题有什么不同?
  • 他获取了窗口对象 globle,我只是将其本地化,如果需要,您可以测试此代码。
  • 对 @AshishKakkad 我同意你的看法。
  • @Jecky 没关系,全球也很好,兄弟。
  • 是的,你是对的,我并不是说你错了。但是我刚刚在我的项目中添加了这 5 行代码,并且 View 就在那里。那么这有什么问题呢兄弟。你能辩解吗?
【解决方案2】:

您不应该将视图直接附加到窗口,将其添加到 ViewController。

试试这样的:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        // without autolayout

        let header = UIView(frame: CGRectMake(0,0, view.bounds.size.width, 50))
        header.backgroundColor = UIColor.redColor()
        view.addSubview(header)

        // with auto layout
        let footer = UIView()
        footer.backgroundColor = UIColor.blackColor()
        footer.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(footer)


        NSLayoutConstraint(item: footer, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50).active = true
        NSLayoutConstraint(item: footer, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1.0, constant: 0).active = true
        NSLayoutConstraint(item: footer, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1.0, constant: 0).active = true
        NSLayoutConstraint(item: footer, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0).active = true


    }

这里有两种附加视图的方法,一种是自动布局,另一种是固定大小和位置。

如果您想添加复杂的视图,最好创建一个 nib.. 一个在情节提要中可以在需要时加载的视图。

如果您需要此视图出现在许多地方,您可以查看而不是将其附加到父视图,可能类似于导航控制器或分页视图控制器,但您需要管理何时应该和不应该显示出来。

【讨论】:

    【解决方案3】:

    我认为您正在将您创建的视图添加到window。您需要将该视图显示为您当前的内容视图,例如,

     self.view.addSubView(self.vwVideo)
    

    而且你不需要获取它的窗口和边界,你可以简单地实现它,

     let bottomView: UIView = UIView(frame: CGRectMake(0, self.view.frame.size.height - 135, self.view.frame.size.width, 135))
     self.view!.addSubview(bottomView)
    

    希望这会有所帮助:)

    【讨论】:

      【解决方案4】:

      最好使用 AutoLayout。 将您的视图添加到 superview

      view.addSubview(footerView)

      然后使用禁用掩码到约束的自动转换

      footerView.translatesAutoresizingMaskIntoConstraints = false

      然后使用以下代码行添加约束

      view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view":footerView]))

      view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(135)]-0-|", options: [], metrics: nil, views: ["view":footerView]))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 2018-03-05
        • 2017-05-11
        • 1970-01-01
        相关资源
        最近更新 更多