开发者

UISearchBar - Keyboard NOT displaying when touched

开发者 https://www.devze.com 2023-03-20 23:06 出处:网络
The keyboard is not being displayed when I touch the UISearchBar in my iphone app.Do I have to use something like [searchBar becomeFirstResponder] in touchesBegan?

The keyboard is not being displayed when I touch the UISearchBar in my iphone app. Do I have to use something like [searchBar becomeFirstResponder] in touchesBegan?

I have the searchBarShouldBeginEditing returning YES.

I have tried using [seachBar becomeFirstResponder] and my delegate gets called as I expect. I see there is a UISearchDisplayController but I thought perhaps a UISearchBar was easy enough to implement.

My custom controller class is a subclass of UIViewController.

UIImageView *topImage = [[UIImageView alloc] initWithImage:
                        [UIImage imageWithContentsOfFile:
                         [myBundle pathForResource:@"top_header"  
  ofType:@"png"inDirectory:@"Images/facebook_friend"]]];

CGRect  viewRect = CGRectMake(0, 0, 320, 480);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, 320, 310) 
       style:UITableViewStylePlain];
self.tableView.bounces = YES;

UIImageView *botto开发者_如何学GomImage = [[UIImageView alloc] initWithImage:
                         [UIImage imageWithContentsOfFile:
                          [myBundle pathForResource:@"bottom_background" ofType:@"png" 
                inDirectory:@"Images/facebook_friend"]]];
bottomImage.center = CGPointMake(bottomImage.center.x, 480-bottomImage.frame.size.height);
// so that we can push a button inside it


self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70,320,0)];
[searchBar setPlaceholder:@"Search"];
[searchBar setDelegate:self];


tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = dataSource;
[tableView reloadData];

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 76, 43)];
backButton.center = CGPointMake(210, 40);
[backButton setBackgroundImage:[UIImage imageNamed:@"Images/facebook_friend/back-button.png"]
     forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backClick:) 
     forControlEvents:UIControlEventTouchUpInside];

// spinner to show we're loading
self.spinner = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(125,100,50,50)];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[tableView addSubview:spinner];

// top search section
[myView addSubview:topImage];
[topImage addSubview:searchBar];

// friend list
[myView addSubview:tableView];

// bottom button section
[bottomImage addSubview:backButton];
[bottomImage addSubview:postButton];
[myView addSubview:bottomImage];

[topImage setUserInteractionEnabled:YES];
[bottomImage setUserInteractionEnabled:YES];
[myView setUserInteractionEnabled:YES];

self.view = myView;

Edit: Sun 9:50am

If I take all the subviews out and just add myView with the searchBar, I only see touch events for myView. However if I just set

self.view=searchBar

then touching the search bar brings up the keyboard. Here is a very small example:

CGRect  viewRect = CGRectMake(0, 0, 320, 480);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70,320,0)];
[searchBar setPlaceholder:@"Search"];

[myView addSubview:searchBar];
self.view = myView;


The height of my UISearchBar was 0 and this is why it wasn't displaying the keyboard when I clicked.

0

精彩评论

暂无评论...
验证码 换一张
取 消