【问题标题】:UITabBarController turns Black while switching tabs切换标签时 UITabBarController 变黑
【发布时间】:2023-03-23 17:08:01
【问题描述】:

我在 UINavigationController 中嵌入了 UITabBarControllerUITabBarController 包含 3 个不同的选项卡,它们通向 3 个不同的显示 UITableView 的 UIViewControllers。我通过情节提要实现了所有这些。在第一个 UIViewController 中,我以编程方式为 UITableView 实现了一个 UISearchController,它正在工作。 当我搜索特定单元格然后切换选项卡时,视图会变黑。 我认为这个问题与 SearchController 有一些关系,因为在包含 SearchController 之前,我没有任何问题。

我在这里解决了几个其他问题,但没有一个可以解决我的问题。

已编辑:

以下是我与 SearchController 相关的代码。

@interface

@property (nonatomic, strong) UISearchController *searchController;
@property BOOL searchControllerWasActive;
@property BOOL searchControllerSearchFieldWasFirstResponder;

@implementation

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;

【问题讨论】:

  • 请分享您的代码
  • 我应该分享哪一部分的示例代码?
  • 请应用下面的代码我回答你的问题。我也遇到了同样的黑屏问题
  • 我使用 StoryBoard 创建了 UITabBarController,它在没有 SearchController 的情况下工作。

标签: ios objective-c uinavigationcontroller uitabbarcontroller uisearchcontroller


【解决方案1】:

UITabBarController 应该是故事板/代码中窗口上最顶层的元素,您可以将导航控制器和视图控制器添加到 UITabBarController 视图控制器。 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITabBarController_Class/index.html

【讨论】:

【解决方案2】:

使用这个示例代码它肯定会帮助你在这里聊天列表,联系人列表和查找朋友是 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"]];


    }

【讨论】:

  • 我认为这个问题与 SearchController 有一些关系,因为在包含 SearchController 之前,我没有任何问题。我已经用代码示例编辑了我的问题。提前致谢。
【解决方案3】:

自己想出了答案。我不知道这是否是正确的做法,但现在该应用程序似乎可以正常工作。

我将我的 UITabBarController 作为初始视图并为每个选项卡单独的 UINavigationControllers。

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多