0
我有一个UITextField,它在UISearchController中,当显示视图控制器时需要成为FirstResponder。UISearchController中的UITextField BecomeFirstResponder
在ViewDidLoad上构建了UISearchController。
在ViewDidAppear我从UISearchController得到的UITextField,并告诉它BecomeFirstResponder
public override void ViewDidLoad(bool animated)
{
base.ViewDidLoad();
BuildSearchController();
SearchController.SearchBar.BecomeFirstResponder();
}
private void BuildSearchController()
{
UIFont MyFont = UIFont.FromName("Helvetica", 20f);
SearchController = new UISearchController(ResultsTableController)
{
WeakDelegate = this,
DimsBackgroundDuringPresentation = false,
WeakSearchResultsUpdater = this,
};
SearchController.SearchBar.ScopeButtonTitles = new string[5] { "Account", "Name", "Service Address", "Phone", "SSN" };
UITextField SearchField = (UITextField)SearchController.SearchBar.Subviews[0].Subviews[2];
SearchField.Font = MyFont;
SearchController.SearchBar.ShowsCancelButton = false;
SearchController.SearchBar.SetScopeBarButtonTitle(new UITextAttributes() { Font = MyFont }, UIControlState.Normal);
SearchController.SearchBar.SizeToFit();
SearchController.SearchBar.SelectedScopeButtonIndex = 1;
SearchController.SearchBar.SetShowsCancelButton(false, false);
UISegmentedControl TempSegControl = SearchController.SearchBar.Subviews[0].Subviews[0].Subviews[1] as UISegmentedControl;
TempSegControl.BackgroundColor = UIColor.FromRGBA(0, 122, 255, 255);
TempSegControl.TintColor = UIColor.White;
TempSegControl.Layer.BorderWidth = 1;
TempSegControl.Layer.CornerRadius = 6;
TempSegControl.Layer.BorderColor = UIColor.White.CGColor;
SearchController.SearchBar.BarTintColor = UIColor.FromRGBA(232, 232, 232, 255);
SearchController.HidesNavigationBarDuringPresentation = false;
SearchField.BecomeFirstResponder();
TableView.TableHeaderView = SearchController.SearchBar;
SearchController.SearchBar.WeakDelegate = this;
}
public override void ViewDidAppear(bool animated)
{
UITextField SearchBox = SearchController.SearchBar.Subviews[0].Subviews[2] as UITextField;
if (SearchBox.CanBecomeFirstResponder)
{
SearchBox.BecomeFirstResponder();
}
base.ViewWillAppear(animated);
}
的UITextField永远不会成为第一个响应者,因为它是不能够成为第一个响应者。为什么UITextField不能成为第一个转型者?
我将该调用移至ViewDidAppear方法。我的UISearchController中的UITextField仍然没有焦点。当显示控制器时,我希望用户能够在无需触摸UITextField内部的情况下开始输入 –
感谢您的工作。这是它的C#版本。 [Export(“didPresentSearchController:”)] public virtual void DidPresentSearchController(UISearchController searchController) { SearchController.SearchBar.Subviews [0] .Subviews [2] .BecomeFirstResponder(); } –