繁体   English   中英

是否可以更改搜索栏中取消按钮的垂直位置?

[英]Is it possible to change the vertical position of cancel button in the search bar?

当我将应用程序从 10 更新到 11 iOS SDK 时,我将搜索栏从UIViewUIViewController navigationItem 此外,我设置在UISearchBar以垂直偏移定位背景图像,因为徽标和 searchBar 之间的距离很小。

搜索未激活

searchBar.setSearchFieldBackgroundImage(searchFieldImage, for: .normal)
searchBar.searchFieldBackgroundPositionAdjustment = UIOffset(horizontal: 0, vertical: 4)

但我不能将取消按钮定位在垂直位置。 如何设置UISearchBar的 Cancel 按钮或按钮中的标题的垂直位置?

搜索处于活动状态

访问取消按钮如下:-

UIBarButtonItem *cancelButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];

现在尝试更新cancelButton的框架(y 轴)。

let cancelButton = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
let offset = UIOffset(horizontal: 0, vertical: 7)
cancelButton.setTitlePositionAdjustment(offset, for: .default)

也看看这里的苹果文档。

您可以增加 searchBar Height heightAnchor.constraint以增加导航栏高度以修复图像标志问题,请检查它

在 IOS 11

searchController.searchBar.heightAnchor.constraint(equalToConstant: 50).isActive = true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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