Salah
Salah

Reputation: 511

How to set initial focus to a button in a viewController from a UITabBarController?

I have a basic application with a UITabBarController which contains 2 UIViewControllers. The first ViewController has a UIButton in it.

I'm trying to get the tabBar menu hidden and the button focused when the app launches. I set the tabBar as hidden, and it does not show on launch as expected. Unfortunately, the button doesn't have the focus.

From what I understand from Apple doc, the "focus chain" engine will set focus on the first visible and focusable item in the window hierarchy.

Can anyone help me on this ? Thank you.

Upvotes: 1

Views: 1745

Answers (1)

creative_rd
creative_rd

Reputation: 695

Solution 1. I think what you can do :

1.Subclass or Customize TabBarController and set set first launch = true in viewdidload of tabController

> override weak var preferredFocusedView: UIView? {   if
> (self.firstLaunch) {
>     self.firstLaunch = false;
>     return self.selectedViewController!.preferredFocusedView;   }   else {
>     let view  = super.preferredFocusedView
>     return view;   }

Hope it helps.It worked for me though

Your focus engine takes the rootView controller as TabBarController and then asks for preferred focus view which is returned as UITabBarItem we need to unfocus that subclassing the class and returning canBecomeFocus to NO.

if you want to change the focus of the element in the firtsViewController then you can overrirde func preferredFocusView and reutrn the view you want to be focussed else preferredFocusView

Solution 2. Just set the root View controller as the HomeViewController without the TabBarController and embed the TabBarController from the second page. This is because anyways you don't need to use TabBar on the first page why need to take care of its focus.

TabBar is set to hidden if you hide from the AppDelegate or the view which is loaded but it has the focus.

Upvotes: 2

Related Questions