UISearchBar - 触摸时键盘不显示

时间:2011-07-16 16:22:19

标签: iphone objective-c uisearchbar

在我的iphone应用程序中触摸UISearchBar时,未显示键盘。我是否必须在touchesBegan中使用类似[searchBar becomeFirstResponder]的内容?

我有searchBarShouldBeginEditing返回YES。

我尝试过使用[seachBar becomeFirstResponder],我的委托会按照我的预期被调用。我看到有一个UISearchDisplayController,但我想也许UISearchBar很容易实现。

我的自定义控制器类是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 *bottomImage = [[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;

编辑:太阳9:50上午

如果我取出所有子视图并只使用searchBar添加myView,我只会看到myView的触摸事件。但是,如果我只是设置

self.view=searchBar

然后触摸搜索栏会弹出键盘。这是一个非常小的例子:

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;

1 个答案:

答案 0 :(得分:0)

我的UISearchBar的高度为0,这就是我点击时没有显示键盘的原因。