[英]Navigation Bar Subview with Xib
I'm running into a slight problem in an app I'm working on. 我正在努力的应用程序中遇到一个小问题。 I would like to extend the navigation bar to support a Search Field like control. 我想扩展导航栏以支持像控件一样的搜索字段。 A user would click a search button in the top right button on the navigation bar and then the navigation bar would expand exposing a Search field that has a search and cancel button. 用户可以单击导航栏右上角的搜索按钮,然后导航栏将展开,显示具有搜索和取消按钮的搜索字段。 I've achieved the following using this implementation: 我使用此实现实现了以下目标:
#import <UIKit/UIKit.h> //Xib header
@interface SearchForm : UIView
@property (weak, nonatomic) IBOutlet UITextField *txtSearchField;
@property (weak, nonatomic) IBOutlet UIButton *btnCancel;
- (IBAction)btnCancel:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnSearch;
- (IBAction)btnSearch:(id)sender;
@end
#import "SearchForm.h" //Xib implementation
@interface SearchForm () <UITextFieldDelegate>
{
}
@end
@implementation SearchForm
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
self.txtSearchField.delegate = self;
}
return self;
}
- (IBAction)btnCancel:(id)sender
{
DLog();
}
- (IBAction)btnSearch:(id)sender
{
}
#pragma mark Text Field Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//TODO:Callback to feed.
return YES;
}
//Main View Controller
-(SearchForm *)feedSearchForm
{
if(!_feedSearchForm)
{
_feedSearchForm = [[[NSBundle mainBundle] loadNibNamed:@"SearchForm" owner:self options:nil] objectAtIndex:0];
//I think this frame is the problem but I'm not sure
_feedSearchForm.frame = CGRectMake(0, self.navigationController.navigationBar.frame.size.height, 320, STREAM_LIST_TOP_CONSTRAINT_HEIGHT);
}
return _feedSearchForm;
}
//Exposes the search field
-(void)expandSearchField:(id)sender
{
tableVerticalVariableConstraint.constant = STREAM_LIST_TOP_CONSTRAINT_HEIGHT;
[UIView animateWithDuration:0.4 animations:^{
[self.navigationController.navigationBar addSubview:self.feedSearchForm];
[self.view layoutIfNeeded];
}];
}
The control contains a UITextField inside. 该控件包含一个UITextField。
The Problem 问题
However, even though this code does exactly what I want, when I click inside the text field, the keyboard never displays. 但是,即使此代码完全符合我的要求,当我在文本字段内单击时,键盘也不会显示。 It's like the touch isn't being registered. 这就像触摸没有被注册。 I tried commenting out the code where I explicitly set the frame for the xib and when I do this, the touch event is recognized, but the xib sits on top of the navigation bar and does not look like a natural extension of the bar which is what I need. 我试着注释掉我明确设置xib框架的代码,当我这样做时,触摸事件被识别,但是xib位于导航栏的顶部,看起来不像是条形图的自然扩展。我需要的。 Any advice or tips would be appreciated. 任何建议或提示将不胜感激。
您可以尝试将以下代码添加到“ - (SearchForm *)feedSearchForm”方法中。
[_feedSearchForm.txtSearchField becomeFirstResponder];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.