iOS 13中的Objective-C应用程序崩溃(搜索栏问题)

时间:2019-10-02 09:31:48

标签: ios objective-c ios13

请有人帮助我,我对Objective-c不熟悉。应用在iOS 13中崩溃 在控制台中发现以下错误

*由于未捕获的异常'NSGenericException'而终止应用程序,原因:'搜索栏布局的视图缺失或分离。应用程序不得从层次结构中删除>。'

搜索条形码

 UIImageView *searchBackView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 45, 320, 30)];
    searchBackView.image = [UIImage imageNamed:@"search1.gif"];
    searchBackView.userInteractionEnabled=YES;
    searchBackView.backgroundColor=[UIColor clearColor];
    [tableHeader addSubview:searchBackView];

  searchBar=[[[UISearchBar alloc] init] autorelease];
    searchBar.delegate=self;
    [searchBar setKeyboardType:UIKeyboardTypeDefault];
    searchBar.barStyle = UISearchBarIconClear;
    searchBar.frame=CGRectMake(17, 3, 285, 30);
    [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search1.gif"] forState:UIControlStateNormal];
    searchBar.backgroundColor=[UIColor clearColor];
    [searchBackView addSubview:searchBar];
    [searchBackView release];
    [tableHeader addSubview:label];

        for (UIView *subView in self.searchBar.subviews)
        {
            for (UIView *secondLevelSubview in subView.subviews){
        if ([secondLevelSubview isKindOfClass:[UIImageView class]]){
            [secondLevelSubview removeFromSuperview];
        }
            }
        }



    searchField = nil;
        for (UIView *subView in self.searchBar.subviews)
        {
            for (UIView *secondLevelSubview in subView.subviews){
                if ([secondLevelSubview isKindOfClass:[UITextField class]])
                {
                    searchField = (UITextField *)secondLevelSubview;
                    break;
                }
            }
        }

        for (UIView *subview in searchBar.subviews)
        {
            for (UIView *secondLevelSubview in subview.subviews){
                if ([secondLevelSubview conformsToProtocol:@protocol(UITextInputTraits)])
                {
                    [(UITextField *)secondLevelSubview setClearButtonMode:UITextFieldViewModeNever];
                }
            }
        }
 if (searchField) {
    searchField.leftViewMode = UITextFieldViewModeNever;
    }
    BluemenAppDelegate *delegate=(BluemenAppDelegate*)[[UIApplication sharedApplication] delegate];
    if (isShowlist==YES) {
        delegate.search_string = @"";
        searchBar.text = delegate.search_string;
        isShowlist = NO;
    }

        searchBar.text = delegate.search_string;

更新

现在我可以启动应用程序,但是现在,如果单击应用程序中的任何文本字段,它会崩溃并给出 XPC连接中断错误

1 个答案:

答案 0 :(得分:0)

如果我的理解正确,您正在尝试实现具有清晰背景的SearchBar。如错误消息所示,iOS 13不允许删除UISearchBarBackground。您可以尝试以下解决方法:

这将使搜索背景图像变得透明,而不会将其删除。

for (UIView *subView in self.searchBar.subviews) {
    for (UIView *secondLevelSubview in subView.subviews){
        if ([secondLevelSubview isKindOfClass: [UIImageView class]]) {
            secondLevelSubview.alpha = 0.0;
            break;
        }
    }
}

只需用这一部分替换代码的这一部分,并检查它是否符合您的期望。