代码之家  ›  专栏  ›  技术社区  ›  Sonic Master

在导航栏中添加UIView

  •  0
  • Sonic Master  · 技术社区  · 8 年前

    我想创建 UINavigationBar 带圆角。看起来是这样的 rounded navigation bar

    我想的是我加上 UIView

    let roundView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
    roundView.backgroundColor = UIColor.whiteBackground
    roundView.roundCorners(corners: [.topLeft, .topRight], radius: 20)
    navigationController?.navigationBar.insertSubview(roundView, at: 0)
    setTitleTextColor(color: UIColor.black)
    

    在用户界面上,这很有效。但是我的 UIBarButtonItem 缺少,它被我的自定义视图覆盖,无法单击。所以我的问题是,如何在导航栏中添加子视图?

    3 回复  |  直到 8 年前
        1
  •  1
  •   Totka    8 年前

    只是不要使用导航栏,而是从头开始创建。是最简单的方法。否则,您可以尝试使用图案图像:

    navigationController?.navigationBar.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
    
        2
  •  0
  •   dahiya_boy    8 年前

    从情节提要,

    1. 您有带有navigationController的ViewController

    enter image description here

    1. navigationController 并取消选择以下选定选项,即显示导航栏可见性。

    enter image description here

    1. 查看带有约束的UIView(purpleView)

      • 顶部,前导-尾随=0 w.r.t.superview
    2. 在带有约束的purpleView中获取另一个UIView(whiteView)

      • 顶部=20(用于状态栏)
    3. 现在将取消和标签添加到您的whiteview

    4. 现在,您的UI层次结构如下所示

    enter image description here

    enter image description here

    就是这样。

    如果你不使用故事板,那么你也可以对代码执行同样的操作。在这种情况下,您必须设置purpleView和whiteView的框架,而不是约束。

    希望你现在明白了。

        3
  •  0
  •   Cloud Music    4 年前

    如何将其作为一个普通的UIView,隐藏导航栏并在下一个VC中显示,谁会知道你使用了哪种技巧。 read this short article here