【问题标题】:How to tap on searchbar to appear the keyboard automatically when view appears without doing anything in iOS, objective C当视图出现而不在iOS中做任何事情时如何点击搜索栏以自动出现键盘,目标C
【发布时间】:2016-06-14 12:30:19
【问题描述】:

在我的 iOS 应用程序中有两个视图。在第一个视图中有一个按钮。当用户按下按钮时,它导航到带有搜索栏的第二个视图控制器。在该视图控制器中,用户必须点击搜索器以获取键盘。我想要的是点击搜索器,并在视图出现时自动弹出键盘给用户而不做任何事情。

我尝试了以下,

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.fromportSearchcontroller setActive:YES];
    [self.fromportSearchcontroller.searchBar becomeFirstResponder];
}

但这没有用。希望您对此有所帮助。

注意: 这就是我创建uisearchcontroller

的方式
- (void)settinguptheSearchBarforfromairports
{
    self.fromportSearchcontroller = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.fromportSearchcontroller.searchResultsUpdater = self;
    self.fromportSearchcontroller.searchBar.delegate = self;
    self.definesPresentationContext = YES;
    self.fromaiportsTableView.tableHeaderView = self.fromportSearchcontroller.searchBar;
    self.fromportSearchcontroller.searchBar.barTintColor = [UIColor lightGrayColor];
    self.fromportSearchcontroller.dimsBackgroundDuringPresentation = NO;
    //self.fromportSearchcontroller.active = YES;
    //self.fromportSearchcontroller.searchBar.text = @"type city or airport";
    [self.fromportSearchcontroller.searchBar sizeToFit];


}

我在 'ViewDidLoad' 方法中调用了这个方法。

  • 搜索栏已激活,但键盘未自动出现(弹出)。

【问题讨论】:

标签: ios objective-c uisearchbar uikeyboard


【解决方案1】:

不要使用UISearchController,而是尝试UISearchBar。对我来说效果很好。

-(void)viewDidAppear:(BOOL)animated
    {
       [self.searchbar becomeFirstResponder];
    }

确保在调用 becomeFirstResponder 之前分配了 self.searchbar

- (void)settinguptheSearchBarforfromairports
{
        self.searchbar = [[UISearchBar alloc]init];
        self.searchbar.backgroundColor = [UIColor whiteColor];
        self.searchbar.tintColor = [UIColor whiteColor];
        self.searchbar.placeholder = @"Search";
        self.searchbar.delegate=self;
        self.searchbar.barTintColor = [UIColor whiteColor];
        self.searchbar.layer.borderWidth = 0;
        [self.searchbar setTranslucent:YES];
        [self.view addSubview:self.searchbar];
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多