UISearchBar not visible until scroll down

I added a UISearchBar programmatically to my tableHeaderView but it doesn't show on view load. There's just a blank space above my table content. When I scroll down a little bit, it appears. I changed the background color of the tableHeaderView to see if it visible at all - I could see the changed color, so it seems the searchBar itself is invisible. Any ideas?

Here's my code:

- (void)viewDidLoad
{
[super viewDidLoad];
...
// Initialize the refresh control.
[self.refreshControl addTarget:self
                        action:@selector(refreshData)
              forControlEvents:UIControlEventValueChanged];
// Suchelemente erzeugen
self.searchBar = [UISearchBar new];
self.searchDC = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDC.searchResultsDelegate = self;
self.searchDC.delegate = self;
self.searchDC.searchResultsDataSource = self;
self.tableView.tableHeaderView = self.searchBar;
// some attempts to make it visible
self.searchBar.hidden = NO;
self.searchBar.layer.hidden = NO;
[self.searchBar sizeToFit];
}

Ok, found out, that the refresh control causes the problem. In viewWillAppear a method is called which updates the title of the refresh control. When I comment this line, the searchbar appears:

self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:title];

So, how to make them both work??


Create a refresh control and set this as tableview's refresh control. In this way, the attributed text of the self.refreshcontrol is not set.

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:GetLocalizedString(@"REFRESH_TO_RELEASE_TITLE", nil)];
[refresh addTarget:self action:@selector(refreshViewPulled:) forControlEvents:UIControlEventValueChanged];
self.refreshControl=refresh;

The issue is because your trying to add 2 things to the same spot because you never really specified a frame for either, even though the UIResultsController knows where it goes, it can be altered, and the UIControlEventValueChanged [dragging down] is probably causing discrepancies with the frames changing. So I would suggest implementing your search bar through storyboard since you stated you are using it. Drag and drop a Search Display Controller below the Navigation Bar but above the Table View, you will see your cursor change to a green plus, that means you can drop it there (which in this case is the tableViewHeader):

PIC1

Then you should be able to continue pragmatically altering the UIRefreshControl per normal

-(void)viewDidLoad {
    self.refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl.backgroundColor = [UIColor clearColor];
    self.refreshControl.tintColor = [UIColor purpleColor];
    [self.refreshControl addTarget:self
                        action:@selector(refreshView:)
              forControlEvents:UIControlEventValueChanged]
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull down to Refresh.."];
}

Now there should be no conflicting frame changes to two items added to the same location.


I had the same problem. I fixed it by pragmatically adjusting the content offset so that rubber band scrolling animation is triggered, causing the UISearchBar to reappear.

If it is invisible, try this:

[self.tableView setContentOffset:CGPointMake(0, -1) animated:NO]; 
链接地址: http://www.djcxy.com/p/55976.html

上一篇: 检查一个单选按钮是否被选中jquery

下一篇: UISearchBar在向下滚动之前不可见