简体   繁体   中英

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.

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. Any advice or tips would be appreciated.

您可以尝试将以下代码添加到“ - (SearchForm *)feedSearchForm”方法中。

 [_feedSearchForm.txtSearchField becomeFirstResponder];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM