【问题标题】:Add UINavigation Back button in UICollectionView with Swift 3使用 Swift 3 在 UICollectionView 中添加 UINavigation Back 按钮
【发布时间】:2017-05-25 20:51:42
【问题描述】:

我用代码在集合视图控制器中添加左导航返回按钮。

//Add Navigation Bar

    navbar.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin, .flexibleRightMargin]
    navbar.delegate = self

    UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green:49.0/255.0, blue:79.0/255.0, alpha:0.1)
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().isTranslucent = true
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

    navItem.title = prefs.value(forKey: "PROVIDER_NAME") as! String?

    let image = UIImage(named: "back_image")
    navItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(addTapped))

    navItem.leftBarButtonItem?.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0)



返回按钮离左边很近。我想从左边添加大约 10px 的填充。
所以,我把代码改成了

 navItem.leftBarButtonItem?.imageInsets = UIEdgeInsetsMake(0, 15, 0, 0)

但它不起作用,并且图像后退按钮看起来更小。如何在“返回”按钮的左侧添加空间?

【问题讨论】:

标签: swift3 uicollectionview uinavigationbar uinavigationitem


【解决方案1】:

我建议将UINavigationBar 替换为简单的UIView。这样您就可以完全控制导航栏的布局。它只不过是一个透明的UIView,里面有一个后退按钮和一个标题标签。就这么简单。

真正的UINavigationBar不止于此。它旨在管理一堆UINavigationItem 对象。它会根据当前项目进行自我调整,并且知道如何进行动画(甚至是交互式)从一种状态到另一种状态的转换。这就是为什么你不能改变酒吧的外观。您不应将其视为常规视图。

更新

实现这一点的另一种方法有点棘手。您可以完全从情节提要中实现它,并且不需要弄乱外观。

  1. UINavigationBar 添加到视图控制器。
  2. UINavigationBar左侧添加一个普通的UIView,使其背景颜色完全透明。
  3. UIButton添加到上一步添加的视图中,并设置一个后退图标作为其图像。
  4. 为按钮添加约束以使其与其父视图的右侧对齐。
  5. 调整视图的宽度,使后退按钮的位置恰好位于您想要的位置。

这是情节提要中的视图层次结构:

这就是你的UINavigationBar 的样子(对你来说,背景是透明的):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多