0

I'm using the newest version of Xcode and Swift.

I have a Navigation Bar with a Navigation Item Title.

On some devices with smaller screens this title is too long so it's getting truncated.

My idea is:

  1. First I count how many characters fit on a specific width. (Of course with an allowance of 2-3 characters, because some characters are bigger than others.)

  2. I have to possible titles, the one is longer with a width of x characters, the other is the shorter alternate with a width of y characters.

  3. Now I check if the longer title will fit depending on its character count and the maximum count of characters the current screen can take. If not, I'll present its shorter alternate.

I want to do the measurement of the screen width with the following code:

let screenWidth = UIScreen.main.bounds.size.width
  1. Is this the right one to do this? Or will this e.g. output different values depending on screen resolution, although the real width of the device is the same …

  2. What unit is this? Pixels?

Note: The app is in portrait mode, so width is also the real devices width.

David
  • 2,898
  • 3
  • 21
  • 57
  • "The app is in portrait mode" Does your app never go into landscape mode? If it does, then the navigation bar is going to be bigger on landscape mode. – Sweeper Jun 26 '20 at 06:55
  • @Sweeper I know, I'll handle but this is not relevant for the question. Can you help me with my questions above? – David Jun 26 '20 at 06:57

1 Answers1

1

What unit is this? Pixels?

From the docs:

The bounding rectangle of the screen, measured in points.

So it's points.

Is this the right one to do this?

Since you are trying to get the navigation bar's width, why not just get the navigation bar's width?

navigationController?.navigationBar.bounds.width

This is going to be the same value as UIScreen.main.bounds.width when the navigation bar covers the whole width of the screen, but it makes a lot more sense this way, doesn't it?

Also, just FYI, you can check whether the screen width is small with trait collections. Although this is not as accurate as calculating the characters' size, it is the recommended way by Apple when you are trying to adapt your UI to different screen sizes.

if traitCollection.horizontalSizeClass == .compact {
    // small screen width
} else {
    // big screen width
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you for your efforts. There's a `leftBarButtonItem` and a `rightBarButtonItem` in my `Navigation Bar`. So I need the width of `UINavigationItem`. Is this possible? `@IBOutlet weak var navigationItemTitle: UINavigationItem! print(navigationItemTitle.bounds.size.width)` says: `Value of type 'UINavigationItem' has no member 'bounds'` – David Jun 26 '20 at 07:53
  • @David Well, in that case I suggest you check the horizontal size class. Checking the widths of these things are really a [hack](https://stackoverflow.com/questions/21922141/how-to-get-the-width-of-the-title-label-in-uinavigationbar). I don't recommend you do it... – Sweeper Jun 26 '20 at 07:58
  • 1
    @David Uh.. no? [a point is 1/72 of an inch](https://graphicdesign.stackexchange.com/a/207) – Sweeper Jun 26 '20 at 08:00
  • Can't I also subtract the size of the `BarButtonItem`s. – David Jun 26 '20 at 08:02
  • Sorry, I didn't know it's so simple. – David Jun 26 '20 at 08:05
  • @David [Again, it's quite finicky... You'd need to reinvent a lot of wheels.](https://stackoverflow.com/questions/5066847/get-the-width-of-a-uibarbuttonitem) What's wrong with size classes? – Sweeper Jun 26 '20 at 08:06
  • I'm not familiar with size classes at all. Will this give me size of my `UINavigationItem` or will I change the title depending of the device model? – David Jun 26 '20 at 08:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216704/discussion-between-sweeper-and-david). – Sweeper Jun 26 '20 at 08:12