Cannot set UITabBar leadingAccessoryView on tvOS 13

In the Human Interface Guidelines for tvOS, under "Tab Bar Customization" on this page:

https://developer.apple.com/design/human-interface-guidelines/tvos/interface-elements/tab-bars/


It states that:

In addition, you can display a logo or other information when the tab bar is visible by supplying accessory views that appear near the leading and trailing ends of the tab bar.


In the UITabBar docs the the following property is available:

trailingAccessoryView

The view at the trailing edge of a tab bar on tvOS.

Use this property to integrate a custom view at the leading edge of your tab bar interface. Use this view to display a custom logo or give access to custom accessories in your app.

https://developer.apple.com/documentation/uikit/uitabbar/3213944-leadingaccessoryview


And there is a trailing version too:

https://developer.apple.com/documentation/uikit/uitabbar/3213945-trailingaccessoryview


HOWEVER... the property is defined as read only:

var leadingAccessoryView: UIView { get }


So it can not be set directly in code.


I've found no way to set it in a Storyboard file, either directly on a UITabBarController UITabBar, on a UITabBar placed directly on a view controller, or by draging UIViews across onto the UITabBar.


I've also tried setting it by creating a custom UITabBar and overriding that property, like so:

class CustomTabBar: UITabBar {
   
    override var leadingAccessoryView: UIView {
        return UIImageView(image: UIImage(named: "example-image"))
    }
   
}


I've verified that I have set the tab bar in storyboard to my custom one, and it is using the custom one, but the leadingAccessoryView value is never accessed, and the view it returns is never displayed. (I've also tried returning various different views).


-


So... How does one set a leadingAccessoryView on a UITabBar in tvOS?


-


Environment:

Xcode version 11.0 (11A420a)

Project deployment target: tvOS 13.0

Simulator: Apple TV 4K - tvOS 13.0 (17J577)

Accepted Reply

I asked this on StackOverflow (https://stackoverflow.com/q/58164506/404409) and the answer was surprisingly simple/obvious in the end:


You have to add your view as a subview. Example:


let imageView = UIImageView(image: UIImage(named: "example-image"))
tabBar.leadingAccessoryView.addSubview(imageView)


I feel the documentation could perhaps make it a bit clearer those views are container views you put your own stuff in though.

Replies

I asked this on StackOverflow (https://stackoverflow.com/q/58164506/404409) and the answer was surprisingly simple/obvious in the end:


You have to add your view as a subview. Example:


let imageView = UIImageView(image: UIImage(named: "example-image"))
tabBar.leadingAccessoryView.addSubview(imageView)


I feel the documentation could perhaps make it a bit clearer those views are container views you put your own stuff in though.