Ios 带Xib的导航栏子视图
我正在开发的一个应用程序遇到了一个小问题。我想扩展导航栏以支持类似控件的搜索字段。用户将单击导航栏右上角按钮中的搜索按钮,然后导航栏将展开,显示具有搜索和取消按钮的搜索字段。使用此实现,我实现了以下目标:Ios 带Xib的导航栏子视图,ios,objective-c,iphone,Ios,Objective C,Iphone,我正在开发的一个应用程序遇到了一个小问题。我想扩展导航栏以支持类似控件的搜索字段。用户将单击导航栏右上角按钮中的搜索按钮,然后导航栏将展开,显示具有搜索和取消按钮的搜索字段。使用此实现,我实现了以下目标: #import <UIKit/UIKit.h> //Xib header @interface SearchForm : UIView @property (weak, nonatomic) IBOutlet UITextField *txtSearchField; @prop
#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];
}];
}
#导入//Xib头
@界面搜索表单:UIView
@属性(弱,非原子)IBUTextField*txtSearchField;
@属性(弱、非原子)IBUIButton*btnCancel;
-(iAction)btnCancel:(id)发送方;
@属性(弱、非原子)IBUIButton*BTN搜索;
-(IBAction)BTN搜索:(id)发送方;
@结束
#导入“SearchForm.h”//Xib实现
@接口搜索表单()
{
}
@结束
@实施查询表
-(id)initWithCoder:(NSCoder*)aDecoder
{
if((self=[super initWithCoder:aDecoder]))
{
self.txtSearchField.delegate=self;
}
回归自我;
}
-(iAction)btnCancel:(id)发送方
{
DLog();
}
-(iAction)BTN搜索:(id)发送方
{
}
#pragma标记文本字段委托
-(BOOL)textField应返回:(UITextField*)textField
{
//TODO:回调以馈送。
返回YES;
}
//主视图控制器
-(搜索表单*)feedSearchForm
{
如果(!\u feedSearchForm)
{
_feedSearchForm=[[NSBundle mainBundle]loadNibNamed:@“SearchForm”所有者:自选项:nil]对象索引:0];
//我认为这个框架是个问题,但我不确定
_feedSearchForm.frame=CGRectMake(0,self.navigationController.navigationBar.frame.size.height,320,流列表\顶部\约束\高度);
}
返回(feedSearchForm);;
}
//公开搜索字段
-(无效)expandSearchField:(id)发件人
{
tableVerticalVariableConstraint.constant=流\列表\顶部\约束\高度;
[UIView animateWithDuration:0.4动画:^{
[self.navigationController.navigationBar addSubview:self.feedSearchForm];
[self.view layoutifneed];
}];
}
该控件内部包含一个UITextField
问题
然而,即使这段代码完全符合我的要求,当我在文本字段中单击时,键盘也不会显示。好像触摸没有被注册。我试着注释代码,明确设置了xib的框架,当我这样做时,触摸事件被识别,但xib位于导航栏的顶部,看起来不像是我所需要的栏的自然延伸。如有任何建议或提示,将不胜感激 您可以尝试将下面的代码添加到“-(SearchForm*)feedSearchForm”方法中
我在feedSearchForm方法和xib的init子类中都尝试了这种方法,但这两种方法都不起作用。我在其他帖子上读到,你不能做我想做的事情,因为导航栏有一个设定的高度,导航栏上的子视图不一定是视图控制器的一部分。我可能在这一点上错了,但这是我迄今为止学到的。
[_feedSearchForm.txtSearchField becomeFirstResponder];