【问题标题】:Cannot see button when placing UIView in front of Nav Bar将 UIView 放在导航栏前面时看不到按钮
【发布时间】:2019-11-04 21:57:56
【问题描述】:

我有一个带有按钮的 UIView,我使用 UIView 在按钮周围添加了一个圆形边框,该按钮可以正常工作并且定位完美,我希望 UIView 位于导航栏的前面,但是一旦我添加了 UIView在导航栏前面,按钮消失了,但 UIVew 出现在导航栏前面。

let viewBar = UIView()

let userProfileView = UIView()
let userProfileButton = UIButton(type: .custom)

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .red
    setupNextButton()
    setupViewBar()
    setupUserProfileButton()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // To Bring the viewBar in front of the navigation bar        
    self.navigationController?.view.addSubview(viewBar)
}

func setupViewBar() {

    viewBar.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)

    view.addSubview(viewBar)
    addNavigationBarConstraints()
}

func setupUserProfileButton() {

    userProfileButton.setImage(#imageLiteral(resourceName: "profilePictureSmall.png"), for: .normal)
    userProfileButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
    userProfileButton.addTarget(self, action: #selector(profilePictureTapped), for: .touchUpInside)

    userProfileView.layer.cornerRadius = 20
    userProfileView.layer.borderWidth = 1.5
    userProfileView.clipsToBounds = true
    userProfileView.layer.borderColor = UIColor.black.cgColor

    userProfileView.addSubview(userProfileButton)

    //viewBar.addSubview(userProfileView)
    view.addSubview(userProfileView)
    addUserProfileButtonConstraints()

}

这是用户配置文件按钮和视图栏的约束

func addViewBarConstraints() {

    viewBar.translatesAutoresizingMaskIntoConstraints = false
    viewBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    viewBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    viewBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    viewBar.heightAnchor.constraint(equalToConstant: 100).isActive = true

}

func addUserProfileButtonConstraints() {

    userProfileView.translatesAutoresizingMaskIntoConstraints = false
    userProfileView.topAnchor.constraint(equalTo: viewBar.safeAreaLayoutGuide.topAnchor, constant: 5).isActive = true
    userProfileView.trailingAnchor.constraint(equalTo: viewBar.safeAreaLayoutGuide.trailingAnchor, constant: -15).isActive = true
    userProfileView.widthAnchor.constraint(equalToConstant: 40).isActive = true
    userProfileView.heightAnchor.constraint(equalToConstant: 40).isActive = true

}

如何使按钮出现?我也添加了一些图片。

【问题讨论】:

  • 为什么不将userProfileButton 作为UIBarButtonItem 添加到navigationItem
  • 我不这样做,因为我想要一个顶部栏,就像 Snapchat 中的那样视图控制器之间,但顶部栏右侧的按钮从一个视图控制器变为另一个视图控制器。

标签: ios swift uiview uinavigationcontroller


【解决方案1】:

导航栏位于视图堆栈的顶部,因此它隐藏了您的红色 UIView。因此,本质上,您必须将按钮添加到导航栏而不是将其添加到 UIView:

    let barButton = UIBarButtonItem(customView: userProfileButton)
    self.navigationItem.rightBarButtonItem = barButton

【讨论】:

  • 这是我在回复@vpoltave 的评论时所做的评论。我不这样做是因为我想要一个像 Snapchat 中的顶部栏,在 snapchat 中,顶部栏或导航栏中的左按钮,当您在视图控制器之间左右滑动时不会改变,但是按钮顶部栏的右侧从一个视图控制器更改为另一个
  • 那么不要使用导航栏。只需在视图上实现按钮即可导航。
  • 我希望其中一个按钮始终停留在视图中,有点像将元素固定在视图中。当我向左或向右滑动时,其他按钮应该会改变。
  • 您不需要导航栏。只需将相同的按钮添加到不同的视图。我很确定 snapshat 应用不使用导航栏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多