UITabBarController 在切换标签时变黑
UITabBarController turns Black while switching tabs
我在 UINavigationController 中嵌入了 UITabBarController。 UITabBarController 包含 3 个不同的选项卡,这导致 3 个不同的 UIViewControllers 显示 UITableView。我通过故事板实现了所有这些。在第一个 UIViewController 中,我以编程方式为正在运行的 UITableView 实现了 UISearchController。 当我搜索特定单元格然后切换标签时,视图变黑。
我认为这个问题与 SearchController 有一定关系,因为在包含 SearchController 之前,我没有遇到任何问题。
我在这里解决了其他几个问题,但是 none 可以解决我的问题。
已编辑:
下面是我的与 SearchController 相关的代码。
@interface
@property (nonatomic, strong) UISearchController *searchController;
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;
@实现
viewDidAppear()
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tblContactsTable.tableHeaderView = self.searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
//reloads table view according to what is searched.
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
self.searchController.searchBar.showsCancelButton = NO;
使用此示例代码,它一定会对您有所帮助 chatlist、contactlist 和 findfriends 是 3 UIViewControllers
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
_chatlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChatListVC"];
_contactlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListVc"];
_findfriends = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"FindfriendsVC"];
self.tabbarcontroller = [[UITabBarController alloc]init];
self.viewControllers = [NSArray arrayWithObjects:_chatlist,_contactlist,_findfriends, nil];
self.tabBar.frame =CGRectMake(0,44,self.view.frame.size.width,50);
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
item0.title = @"Chat List";
item1.title = @"Contacts";
item2.title = @"Find Friends";
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"chatlist.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"chatlist.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"contacts.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"contacts.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"findfriends.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"findfriends.png"]];
}
UITabBarController 应该是 storyboard/code 中 window 上最顶层的元素,您可以将导航控制器和视图控制器添加到 UITabBarController 视图控制器。
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITabBarController_Class/index.html
我自己想出了答案。我不知道这样做是否完全正确,但现在应用程序似乎可以正常工作了。
我将我的 UITabBarController 作为初始视图并为每个选项卡单独设置 UINavigationControllers。
我在 UINavigationController 中嵌入了 UITabBarController。 UITabBarController 包含 3 个不同的选项卡,这导致 3 个不同的 UIViewControllers 显示 UITableView。我通过故事板实现了所有这些。在第一个 UIViewController 中,我以编程方式为正在运行的 UITableView 实现了 UISearchController。 当我搜索特定单元格然后切换标签时,视图变黑。 我认为这个问题与 SearchController 有一定关系,因为在包含 SearchController 之前,我没有遇到任何问题。
我在这里解决了其他几个问题,但是 none 可以解决我的问题。
已编辑:
下面是我的与 SearchController 相关的代码。
@interface
@property (nonatomic, strong) UISearchController *searchController;
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;
@实现
viewDidAppear()
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tblContactsTable.tableHeaderView = self.searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
//reloads table view according to what is searched.
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
self.searchController.searchBar.showsCancelButton = NO;
使用此示例代码,它一定会对您有所帮助 chatlist、contactlist 和 findfriends 是 3 UIViewControllers
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
_chatlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChatListVC"];
_contactlist = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListVc"];
_findfriends = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"FindfriendsVC"];
self.tabbarcontroller = [[UITabBarController alloc]init];
self.viewControllers = [NSArray arrayWithObjects:_chatlist,_contactlist,_findfriends, nil];
self.tabBar.frame =CGRectMake(0,44,self.view.frame.size.width,50);
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
item0.title = @"Chat List";
item1.title = @"Contacts";
item2.title = @"Find Friends";
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"chatlist.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"chatlist.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"contacts.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"contacts.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"findfriends.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"findfriends.png"]];
}
UITabBarController 应该是 storyboard/code 中 window 上最顶层的元素,您可以将导航控制器和视图控制器添加到 UITabBarController 视图控制器。 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITabBarController_Class/index.html
我自己想出了答案。我不知道这样做是否完全正确,但现在应用程序似乎可以正常工作了。
我将我的 UITabBarController 作为初始视图并为每个选项卡单独设置 UINavigationControllers。