2011-07-16 89 views
0

當我觸摸我的iphone應用程序中的UISearchBar時,鍵盤未顯示。我是否必須在touchesBegan中使用[searchBar becomeFirstResponder]之類的內容?UISearchBar - 觸摸時不顯示鍵盤

我有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 AM

如果我把所有的子視圖出來,只是添加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; 
+0

這真的很奇怪的錯誤,就好像你只是放置搜索欄,並沒有定義委託或任何方法,然後它顯示鍵盤上的搜索欄文本字段。你確定你沒有做一些不尋常的事情嗎?你能張貼一些猜測嗎? XIB截圖? – Deeps

回答

0

我的UISearchBar的高度爲0,這就是爲什麼當我點擊時它沒有顯示鍵盤。