带有 UISearchBar 的 LeftBarButtonItem 在 iOS 11 上不可见

LeftBarButtonItem with UISearchBar is not visible on iOS 11

标题很容易解释,在 iOS 10.3 上使用 UIBarButtonItem 和自定义视图(在本例中 UIStackView)分配给 LeftBarButtonItem NavigationBar 在 iOS 11 上不可见。我还没弄清楚为什么它没有显示,但是当我用键盘键入内容时,我的 TextChanged 事件逻辑起作用了!所以 UISearchView 在那里但不可见:

这是一些代码(它是用 C# 编码的,但它使用的是 Objective C 方法。):

var width = NavigationController.NavigationBar.Frame.Width;
var height = NavigationController.NavigationBar.Frame.Height;
_searchBarContainer = new UIStackView(new CGRect(0, 0, width * 0.75, height))
{
    Alignment = UIStackViewAlignment.Center,
    Axis = UILayoutConstraintAxis.Horizontal,
    Spacing = 3
};

_uiSearchBar = new UISearchBar
{
    BackgroundColor = UIColor.Clear,
    BarTintColor = UIColor.Clear,
    BackgroundImage = new UIImage(),
    Placeholder = Strings.Search
};

_uiSearchBar.SizeToFit();
if (_iOS11)
{
    _uiSearchBar.HeightAnchor.ConstraintEqualTo(44).Active = true;
}

_searchbarButtonItem = new UIBarButtonItem(_searchBarContainer);

NavigationItem.SetLeftBarButtonItem(_searchbarButtonItem, true);
ParentViewController.NavigationItem.LeftBarButtonItem = NavigationItem.LeftBarButtonItem;

在 iOS 10 上使用相同的代码即可。

在将 _searchBarContainer 设置为左​​栏按钮项目之前,请尝试设置约束以正确调整其大小。从 iOS11 起,导航栏使用自动布局。确保仅在存在 iOS 11 时添加约束,否则我在 iOS 9 导航栏中遇到问题。 另请查看 Dev forum 中的此线程,其中解释了条形项如何包装在堆栈视图中,也许也有助于解决您的特定问题。