【问题标题】:Is it possible to change the vertical position of cancel button in the search bar?是否可以更改搜索栏中取消按钮的垂直位置?
【发布时间】:2018-05-17 11:09:20
【问题描述】:

当我将应用程序从 iOS SDK 10 更新到 11 时,我将 UIViewController 中的搜索栏从 UIView 移动到了 navigationItem。 此外,我将背景图像设置为在UISearchBar 中放置一个垂直偏移量,因为徽标和searchBar 之间的距离很小。

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

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

【问题讨论】:

    标签: ios swift uisearchbar ios11 uisearchcontroller


    【解决方案1】:

    按如下方式访问取消按钮:-

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

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

    【讨论】:

    • 谢谢!!它有效,请使用 swift 示例更新您的答案,您可以添加 cancelButton.setTitlePositionAdjustment 方法以获得更多解释答案
    • 能否请您解释得更透彻一些?
    【解决方案2】:
    let cancelButton = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
    let offset = UIOffset(horizontal: 0, vertical: 7)
    cancelButton.setTitlePositionAdjustment(offset, for: .default)
    

    还可以查看苹果文档here

    【讨论】:

      【解决方案3】:

      您可以增加搜索栏高度heightAnchor.constraint 增加导航栏高度来修复图像标志问题,检查一下

      在 IOS 11 中

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-21
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 2011-05-26
        • 1970-01-01
        • 1970-01-01
        • 2018-05-05
        相关资源
        最近更新 更多