首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

How to move right UIBarButtonItem next to largeTitle in UINavigationItem in iOS 13 (如何在largeTitle 13中将鼠标右移至UINavigationItem的旁边)

在iOS 13中,要将右侧的UIBarButtonItem移动到largeTitle旁边,可以使用以下步骤:

  1. 首先,确保你的视图控制器(ViewController)已经嵌入到一个导航控制器(UINavigationController)中。
  2. 在视图控制器的viewDidLoad方法中,创建一个自定义的UIBarButtonItem,并将其添加到导航栏的右侧。
代码语言:txt
复制
override func viewDidLoad() {
    super.viewDidLoad()

    let customButton = UIBarButtonItem(title: "Custom", style: .plain, target: self, action: #selector(customButtonTapped))
    navigationItem.rightBarButtonItem = customButton
}

@objc func customButtonTapped() {
    // 处理按钮点击事件
}
  1. 接下来,我们需要调整largeTitle的位置,使其不覆盖右侧的UIBarButtonItem。可以通过设置导航栏的prefersLargeTitles属性为true来启用largeTitle,并通过设置导航栏的largeTitleDisplayMode属性为.always来保持largeTitle的显示。
代码语言:txt
复制
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode = .always
}
  1. 为了避免largeTitle覆盖右侧的UIBarButtonItem,我们可以在视图控制器的viewDidAppear方法中,获取导航栏的子视图,并将其frame向右移动。
代码语言:txt
复制
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let navigationBar = navigationController?.navigationBar {
        for subview in navigationBar.subviews {
            if NSStringFromClass(subview.classForCoder) == "_UINavigationBarLargeTitleView" {
                subview.frame.origin.x = navigationBar.frame.width - subview.frame.width
            }
        }
    }
}

这样,你就可以将右侧的UIBarButtonItem移动到largeTitle旁边了。

在腾讯云的相关产品中,可以使用腾讯云移动开发套件(https://cloud.tencent.com/product/mks)来构建iOS应用程序,并使用腾讯云对象存储(COS)(https://cloud.tencent.com/product/cos)来存储和管理应用程序的数据。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

扫码

添加站长 进交流群

领取专属 10元无门槛券

手把手带您无忧上云

扫码加入开发者社群

热门标签

活动推荐

    运营活动

    活动名称
    广告关闭
    领券