Search code examples
iosobjective-cios13

Objective-c App crashing in iOS 13(Search bar issue)


Please someone help me I am not familiar with Objective-c. App is crashing in iOS 13 found the following error in console

* Terminating app due to uncaught exception 'NSGenericException', reason: 'Missing or detached view for search bar layout. The application must not remove > from the hierarchy.'

Search bar code

 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;

Update

Now I am able launch app but now if click on any textfield in app it is crashing and giving XPC connection interrupted error


Solution

  • If I understood correctly, you're trying to achieve a SearchBar with a clear background. As seen on the error message, iOS 13 won't allow UISearchBarBackground to be removed. There is a work around you can try:

    This will make the search background image to be transparent, without removing it.

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

    Just replace this part of your code with this one and check if it attends what you are expecting.