Adjusting the width of a UISlider in self.navigationItem.titleView

I set the titleView of a view controller to a UISlider like so in viewDidLoad:

UISlider *slider = [UISlider new];
self.navigationItem.titleView = slider;

The desired outcome is that the slider takes the full width of the title view. This is working fine when the view is loaded in the wider landscape mode. The slider adjust its size as expected when rotating to portrait mode.

However, when the view is loaded in the narrower portrait mode and then the device is rotated to landscape, the slider does not grow in width to make use of the newly available size.

Why is that so and how can it get the desired outcome as described?

After viewDidLoad:

After rotating:

Answered by DTS Engineer in 827852022

You might need to update the sliders frame in viewDidLayoutSubviews . For example:

  override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        guard let navigationBarWidth = navigationController?.navigationBar.frame.width else { return }
        slider.frame = CGRect(x: 0, y: 0, width: navigationBarWidth, height: 30)
     }

It's worth filing a bug report for this using Feedback Assistant. Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

Accepted Answer

You might need to update the sliders frame in viewDidLayoutSubviews . For example:

  override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        guard let navigationBarWidth = navigationController?.navigationBar.frame.width else { return }
        slider.frame = CGRect(x: 0, y: 0, width: navigationBarWidth, height: 30)
     }

It's worth filing a bug report for this using Feedback Assistant. Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

Adjusting the width of a UISlider in self.navigationItem.titleView
 
 
Q