gpt4 book ai didi

ios - 如何在 iOS 13 中的 UINavigationItem 中将 UIBarButtonItem 向右移动到 largeTitle 旁边

转载 作者:行者123 更新时间:2023-11-29 05:36:12 25 4
gpt4 key购买 nike

我注意到 iOS 13 中的一堆系统应用程序在显示展开的导航栏时显示了与大标题一致的 UIBarButtonItems。 (消息、健康、App Store 以及 WWDC 视频中的至少一个演示应用)

滚动时,在标准外观下,这些图标有时会向上移动到标准导航栏(与标题一起,例如在消息中),有时会消失(健康应用程序)。

由于这存在于多个应用程序中,甚至是 WWDC 视频演示应用程序中,因此我认为这是 iOS 13 的一项新功能。

有人知道如何实现这一目标吗?

large title navigation bar with right bar buttons vertically aligned with title

最佳答案

请检查这个示例,这将解决您的问题。

private let imageView = UIImageView(image: UIImage(named: "Image_icon"))

/// WARNING: Change these constants according to your project's design
private struct Const {
/// Image height/width for Large NavBar state
static let ImageSizeForLargeState: CGFloat = 40
/// Margin from right anchor of safe area to right anchor of Image
static let ImageRightMargin: CGFloat = 16
/// Margin from bottom anchor of NavBar to bottom anchor of Image for Large NavBar state
static let ImageBottomMarginForLargeState: CGFloat = 12
/// Margin from bottom anchor of NavBar to bottom anchor of Image for Small NavBar state
static let ImageBottomMarginForSmallState: CGFloat = 6
/// Image height/width for Small NavBar state
static let ImageSizeForSmallState: CGFloat = 32
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
navigationController?.hidesBarsOnSwipe = true
addNavView(const: Const.ImageSizeForSmallState, bottomAnchor: Const.ImageBottomMarginForSmallState)
} else {
navigationController?.hidesBarsOnSwipe = false
addNavView(const: Const.ImageSizeForSmallState, bottomAnchor: Const.ImageBottomMarginForSmallState)
}
}

func addNavView(const: CGFloat = Const.ImageSizeForLargeState, bottomAnchor: CGFloat = Const.ImageBottomMarginForLargeState) {
guard let navigationBar = self.navigationController?.navigationBar else { return }
navigationBar.addSubview(imageView)
// setup constraints
imageView.layer.cornerRadius = const / 2
imageView.clipsToBounds = true
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.rightAnchor.constraint(equalTo: navigationBar.rightAnchor, constant: -Const.ImageRightMargin),
imageView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -bottomAnchor),
imageView.heightAnchor.constraint(equalToConstant: const),
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor)
])
UIView.animate(withDuration: 0.4, delay: 0.2, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
self.imageView.layoutIfNeeded()
}, completion: nil)
}

/image/8QfKC.png

关于ios - 如何在 iOS 13 中的 UINavigationItem 中将 UIBarButtonItem 向右移动到 largeTitle 旁边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57014051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com