3

How can I add a UIlabel to the area in green? (it will be within the navigation bar)

enter image description here

I was able to add the logo as a button image in rightBarButtonItem, but I can't google a way to add a label to the nav bar in this specific position.

After reading around I'm thinking maybe I have to create a custom view that would contain the logo and a UILabel, arrange them vertically within that view, and then place that view in rightBarButtonItem?

edit: This is a unique problem from other answers here in that I am looking for how to add a UILabel to an area of the NavigationBar that is outside of the "rightBarButtonItem" area. Using rightBarButtonItem or setting a custom view to appear within rightBarButtonItem does not achieve what I am looking for. I want a UILabel to appear in the NavBar on the right side aligned vertically with the "Today" title.

dank_muffin
  • 263
  • 2
  • 12
  • Yes, You need to create a custom view for it. – manishsharma93 Aug 07 '19 at 12:48
  • Possible duplicate of [How to add custom view on right of navigation bar Swift?](https://stackoverflow.com/questions/31876060/how-to-add-custom-view-on-right-of-navigation-bar-swift) – Sina Aug 07 '19 at 12:51
  • Thanks! I'll give it a try. – dank_muffin Aug 07 '19 at 12:53
  • This is the right direction but how can I extend the view further down vertically? Right now, it gets cut off by something no matter how large I make the view's Y value. See here: https://imgur.com/a/7BPR1R2 (the custom view background is set to red) – dank_muffin Aug 07 '19 at 13:03

2 Answers2

1

I ended up figuring out that using a custom header within the collectionView is the way to go (rather than trying to place a UILabel into the navigationBar via a custom UIView into the rightBarButtonItem space). This worked for me because I wanted the title and date to disappear upon scrolling anyway. Basically this mimics the "Today" tab in the iOS App Store, and my solution finally achieved what I was looking for.

Finished result:

The 2 posts that helped me set up the collectionView header and attach the labels are here:

  1. How to add UICollectionView Header

  2. Display Section Header UICollectionReusableView

Nimantha
  • 6,405
  • 6
  • 28
  • 69
dank_muffin
  • 263
  • 2
  • 12
0

You can follow this code snippet:

let containerView = UIView()
...

let rightBarButton = UIBarButtonItem(customView: containerView)
navigationItem.rightBarButtonItem = rightBarButton
Davydov Denis
  • 346
  • 1
  • 6
  • Thanks! This works, but How can I get the containerView to extend further down? Right now, no matter how large I set the Y value, it gets cut off by something. See here (I set the view background to red): https://imgur.com/a/7BPR1R2 – dank_muffin Aug 07 '19 at 13:02
  • Oh, I'm not exactly sure, but you can try autoresizingMask with flexible height or flexible bottom margin, or you can try to override intrinsic content size or sizeThatFits method – Davydov Denis Aug 07 '19 at 13:07