Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Template tvOS

Anybody know how to implement search template like in Apple tvOS Human Interface Guidelines, using native development in Objective-C or Swift, without TVML ?

like image 370
Alex Overseer Avatar asked Feb 10 '26 21:02

Alex Overseer


1 Answers

So, after research I was able to find a solution:

Objective - C

If in application is tabBar, i created a subclass from UITabBarController e.g. APTabBarController. In APTabBarController, in method

- (void)viewDidLoad

I do next:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SearchResultsViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"SearchResultsViewController"];
UISearchController *searchController = [[UISearchController alloc] initWithViewController:myViewController];
UISearchContainerViewController *containerVC = [[UISearchContainerViewController alloc] initWithSearchController: searchController];
                                 containerVC.title = @"Search";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: containerVC];

NSMutableArray *newTab = [self.viewControllers mutableCopy];
[newTab addObject: navigationController];        
[self setViewControllers: newTab];

Where:

  1. storyboard - is my storyboard
  2. SearchResultsViewController - is my controller from storyboard that contains collectionView
  3. UISearchController - is controller that allow to find what do you need
  4. UISearchContainerViewController - and these one is like a view controller from tabBarController
  5. In "newTab" - I add fresh created viewController that i need

But, problem that I found is that i can't catch searched text. For that, create a subclass from UISearchController, and implement custom

initWithViewController

In my case it looks like these:

In .h

#import <UIKit/UIKit.h>

@interface SearchExercisesViewController : UISearchController

- (id) initWithViewController:(UIViewController *) viewController;

@end

In .m

#import "SearchExercisesViewController.h"

@interface SearchExercisesViewController () <UISearchBarDelegate>

@property (nonatomic, strong) UIViewController *viewController;

@end

@implementation SearchExercisesViewController

- (id) initWithViewController:(UIViewController *) viewController {
    self = [super initWithSearchResultsController:viewController];
    if (self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchBar.delegate = self;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSLog(@"%@",searchText);
}

@end

Profit, and now, replace

UISearchController *searchController = [[UISearchController alloc] initWithViewController:myViewController];

with

SearchExercisesViewController *searchController = [[SearchExercisesViewController alloc] initWithViewController:myViewController];

All done. Now only that remain is to sent data to viewController that contains collection view, and implement logic for search. For sent data you can you Delegate pattern or NSNotification. You can find how to implement that in that post:

it possible to Pass Data with popViewControllerAnimated?

Swift

In swift is the same, how to do that, you can find on Apple example from these link:

https://github.com/brunogb/TVExamples/tree/master/UIKitCatalogtvOSCreatingandCustomizingUIKitControls

like image 188
Alex Overseer Avatar answered Feb 12 '26 16:02

Alex Overseer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!