簡體   English   中英

使用Xib導航欄子視圖

[英]Navigation Bar Subview with Xib

我正在努力的應用程序中遇到一個小問題。 我想擴展導航欄以支持像控件一樣的搜索字段。 用戶可以單擊導航欄右上角的搜索按鈕,然后導航欄將展開,顯示具有搜索和取消按鈕的搜索字段。 我使用此實現實現了以下目標:

#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];
  }];
}

在此輸入圖像描述

該控件包含一個UITextField。

問題

但是,即使此代碼完全符合我的要求,當我在文本字段內單擊時,鍵盤也不會顯示。 這就像觸摸沒有被注冊。 我試着注釋掉我明確設置xib框架的代碼,當我這樣做時,觸摸事件被識別,但是xib位於導航欄的頂部,看起來不像是條形圖的自然擴展。我需要的。 任何建議或提示將不勝感激。

您可以嘗試將以下代碼添加到“ - (SearchForm *)feedSearchForm”方法中。

 [_feedSearchForm.txtSearchField becomeFirstResponder];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM