Safe and forward compatible
In iOS 15.0 the route button of MPVolumeView
shows the route button as a subview. The safest way to get rid of it without a deprecation warning is to look for the button and hide it:
volume.subviews.first(where: { $0 is UIButton })?.isHidden = true
Unlike just ignoring the warning or setting the value with key-value coding (as suggested by @spasbil), this will not crash the app in a future iOS release where Apple may have removed the deprecated showsRouteButton
. However, it has the disadvantage of leaving an empty space right of the slider.
Setting the value through key-value coding does lead to a visually more satisfying solution, where the slider extends over the full width of the MPVolumeView
. Using a #keypath
expression as suggested by @spasbil shows the same warning in Xcode 13.0. Using a String
literal rather than a #keypath
expression, as suggested in a comment, avoids this warning. It does not resolve the danger of crashing on future iOS releases where showsRouteButton
will have disappeared.
Apple never tells us when they will remove a deprecated property, but they rarely do it in a minor release. So we can hope that showsRouteButton
will stay around at least for iOS 15.* releases. A forward compatible way to get rid of the route button is therefore:
if #available(iOS 16, *) {
volumeView.subviews.first(where: { $0 is UIButton })?.isHidden = true
} else {
volumeView.setValue(false, forKey: "showsRouteButton")
}
We might need to get back to this during the next major release beta season, presumably next summer. In the meantime, use this at your own risk. In the unlikely case that Apple removes the property in a later release of iOS 15.*, your app will crash. In the more likely case that showsRouteButton
stays available but deprecated in iOS 16, your MPVolumeView
will leave some unwanted space until you bump up the availability check.