带有大标题的导航栏 iOS 11 的图像

2024-04-19

AppStore app has an icon with an image on the right side of the NabBar with Large Title: enter image description here

如果有人的话我真的很感激知道如何实施 it or ideas关于如何去做。

BTW: Setting an image for UIButton inside of UIBarButtonItem won't work. Tried already. The button sticks to the top of the screen: enter image description here


经过几个小时的编码,我终于成功了让它起作用。我也决定写一篇详细教程: link https://blog.uptech.team/how-to-build-resizing-image-in-navigation-bar-with-large-title-8ba2e8bcb840。如果您更喜欢非常详细的说明,请遵循它。

Demo: enter image description here

GitHub 上的完整项目:link https://github.com/tungfam/ImageInNavigationBarDemo.

这里有5 steps来完成它:

第 1 步:创建图像

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

第 2 步:添加常量

/// 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
    /// Height of NavBar for Small state. Usually it's just 44
    static let NavBarHeightSmallState: CGFloat = 44
    /// Height of NavBar for Large state. Usually it's just 96.5 but if you have a custom font for the title, please make sure to edit this value since it changes the height for Large state of NavBar
    static let NavBarHeightLargeState: CGFloat = 96.5
}

第三步:设置用户界面:

private func setupUI() {
    navigationController?.navigationBar.prefersLargeTitles = true

    title = "Large Title"

    // Initial setup for image for Large NavBar state since the the screen always has Large NavBar once it gets opened
    guard let navigationBar = self.navigationController?.navigationBar else { return }
    navigationBar.addSubview(imageView)
    imageView.layer.cornerRadius = Const.ImageSizeForLargeState / 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: -Const.ImageBottomMarginForLargeState),
        imageView.heightAnchor.constraint(equalToConstant: Const.ImageSizeForLargeState),
        imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor)
        ])
}

第四步:创建图像调整大小方法

private func moveAndResizeImage(for height: CGFloat) {
    let coeff: CGFloat = {
        let delta = height - Const.NavBarHeightSmallState
        let heightDifferenceBetweenStates = (Const.NavBarHeightLargeState - Const.NavBarHeightSmallState)
        return delta / heightDifferenceBetweenStates
    }()

    let factor = Const.ImageSizeForSmallState / Const.ImageSizeForLargeState

    let scale: CGFloat = {
        let sizeAddendumFactor = coeff * (1.0 - factor)
        return min(1.0, sizeAddendumFactor + factor)
    }()

    // Value of difference between icons for large and small states
    let sizeDiff = Const.ImageSizeForLargeState * (1.0 - factor) // 8.0

    let yTranslation: CGFloat = {
        /// This value = 14. It equals to difference of 12 and 6 (bottom margin for large and small states). Also it adds 8.0 (size difference when the image gets smaller size)
        let maxYTranslation = Const.ImageBottomMarginForLargeState - Const.ImageBottomMarginForSmallState + sizeDiff
        return max(0, min(maxYTranslation, (maxYTranslation - coeff * (Const.ImageBottomMarginForSmallState + sizeDiff))))
    }()

    let xTranslation = max(0, sizeDiff - coeff * sizeDiff)

    imageView.transform = CGAffineTransform.identity
        .scaledBy(x: scale, y: scale)
        .translatedBy(x: xTranslation, y: yTranslation)
}

Step 5:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard let height = navigationController?.navigationBar.frame.height else { return }
    moveAndResizeImage(for: height)
}

希望它很清楚并对您有所帮助! 如果您还有任何其他问题,请在评论中告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有大标题的导航栏 iOS 11 的图像 的相关文章

  • let/var 如何解决可变性? [复制]

    这个问题在这里已经有答案了 我没有任何问题 我只是想对有关可变性的问题进行一些澄清 在 Objective C 中我们会使用例如NSMutableArray得到一个可变数组和NSArray得到一个不可变的 我对两者的内部运作了解不多 但据我
  • 进入后台时 Alamofire 请求卡住?

    我正在使用 Alamofire 调用 Web 服务 该服务需要相当长的时间才能加载 如果应用程序进入后台 当我返回应用程序时 我会被加载程序卡住 我想这是因为调用永远不会向我的完成处理程序返回任何内容 我该如何解决这个问题 您可以使用后台抓
  • iOS 13 检查 CLLocationManager 的临时授权状态

    根据 WWDC 视频 https developer apple com videos play wwdc2019 705 https developer apple com videos play wwdc2019 705 当你要求 Al
  • 保存来自 TrueDepth 相机的深度图像

    我正在尝试保存 iPhone X TrueDepth 相机的深度图像 使用AVCam照片滤镜 https developer apple com library content samplecode AVCamPhotoFilter Lis
  • 二元运算符“/”不能应用于两个(Int)操作数[重复]

    这个问题在这里已经有答案了 我得到了Binary operator cannot be applied to two Int operands当我将以下代码放入 Xcode 中的 Swift Playground 时出错 func sumO
  • 在 github 上下载 ZIP 时没有 .xcodeproj 文件

    我正在尝试将我的 GitHub 项目放入 Xcode 中 当我从 GitHub 下载时 zip 文件不包含任何 xcodeproj 文件 另外 即使我在 xcode 上登录 GitHub 克隆 下载时也没有 在 Xcode 中打开 选项 如
  • iOS Swift 和 reloadRowsAtIndexPaths 编译错误

    我与 xCode Swift 陷入僵局并刷新 UITableView 的单行 这条线有效 self tableView reloadData 而这条线没有 self tableView reloadRowsAtIndexPaths curr
  • WKWebview 中的 iCLoud 文档选择器关闭容器视图

    我有一个 WKWebview 加载基于 Web 的 UI 我希望用户能够从其 iCloud 文档上传文件 我已授予正确的权限 并且可以浏览 iCloud 文档 但是 当我选择文件或单击取消按钮时 文档选择器视图也会关闭 WKWebview
  • 制作已准备好开发人员 ID 的 macOS 安装程序包

    注意 这是为了OS X 安装程序 https en wikipedia org wiki Installer macOS 仅包 提交到的包Mac 应用商店 https en wikipedia org wiki Mac App Store遵
  • 为什么在迭代字典时会出现“类型 [object] 的值没有成员 'lowercaseString'” 错误? [关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我有几个对象 Struct object var title String var one object green v
  • 更改导航项(栏)的背景颜色

    有没有一种简单的方法可以更改视图顶部导航项的背景颜色 我有一个基于导航的应用程序 我只希望一个视图获得另一种背景颜色 我主要使用 IB 创建视图 我找到了以下解决方案 未测试 float r 10 float g 55 float b 13
  • 为 Swift 对象/属性设置观察者

    我一直在寻找一种在连接到 Mac 的显示器数量发生变化时触发方法的方法 我知道我可以获得 NSScreen screens count 的值 但我需要找到一种方法来在该值发生变化时创建通知或其他内容 或者指示所连接的显示器数量发生变化的其他
  • 如何向 UIView 添加大小调整手柄?

    我试图根据用户请求在运行时动态创建视图 UIImageView 和 UITextView 然后允许用户移动它们并调整它们的大小 除了调整大小之外 我的一切都工作得很好 我尝试使用捏合手势识别器 但发现它对于我想要的东西来说太笨拙了 因此 我
  • 如何右对齐 UILabel?

    Remark 实施 myLabel textAlignment right does not解决了我的问题 这不是我所要求的 我想要实现的是让标签对齐右对齐 为了更清楚地说明 这就是如何left对齐外观 就是这样justify对齐外观 if
  • 如何重新定位或移动 Google Maps SDK 上的当前位置按钮?

    如何将 Objective C 中的当前位置按钮移至我的偏好 现在 我已启用它 但底角有东西挡住了它 Thanks 您可以使用 padding 将按钮向上移动 self mapView padding UIEdgeInsets top 0
  • 调用 SwiftUI 中位置 #11、#12 处的额外参数 [重复]

    这个问题在这里已经有答案了 我在 SwiftUI 中的切换开关上不断收到 调用中位置 11 12 处有额外参数 错误 我见过其他人有 调用中的额外参数 错误 但答案似乎没有帮助 另外 我的错误是 位置 11 12 我还没有看到其他人发生这种
  • 从 iOS 13 开始安排 iOS 后台任务

    我正在实现用于更新数据的BackgroundTasks 框架 但我遇到了以下问题 无法计划刷新App 错误域 BGTaskSchedulerErrorDomain代码 1 空 无法安排数据提取 Error Domain BGTaskSche
  • 可以获取位置,但无法获取航向

    我目前只使用模拟器 但我在 iOS 模拟器上快速使用 CoreLocation 时遇到问题 我得到此代码打印的位置更新 但从未得到标题 我不想当然 我正在尝试制作一个指南针类型的应用程序 它将显示目标的方位 class CompassVie
  • NSManagedObject 的 Xcode 9 构建问题:Date 与 NSDate

    Xcode 9 生成不同的代码Date模拟器与设备中实体的类型属性 我有codegen功能下Class set to category extension在核心数据中 直到 Xcode 8 3 最新 一切都工作正常 NSDate总是 下面是
  • 设置/覆盖 UICollectionView 中单元格之间的填充

    我有一个 UICollectionView 但在获取单元格之间的填充时遇到了问题 理论上 我应该能够将屏幕除以 4 并且我可以获得包含 4 个图像的单元格大小 完美地占据屏幕宽度 但是 它选择不这样做 相反 它会创建 3 个具有巨大填充的图

随机推荐

  • IIS7的工作进程是什么?

    我正在尝试在 Visual Studio 2008 中执行 附加到进程 进行调试 但我无法弄清楚要附加到哪个进程 帮助 事实上它仍然是 w3wp exe 您需要检查 显示所有会话中的进程 让它显示的选项 这也让我困惑了一段时间
  • 如果不调用notify(),等待线程会发生什么?

    如果不调用notify 等待线程会发生什么 这是虚假唤醒吗 If a waiting Thread is not notified通过致电notify or notifyAll 在所述线程正在等待的对象上 则可能会发生以下任一情况 the
  • Chrome 调试协议:HeapProfiler.getHeapSnapshot 忽略回调

    我正在开发一个测试套件 作为 Chrome 扩展实现 该套件使用 Chrome Chromium 的远程调试协议以编程方式获取和分析堆快照 因为Profiler 似乎不是公共协议的一部分 我正在使用这一页 http trac webkit
  • HTML 多选框

    我只是想知道下面的表格的名称是什么 我从早上就在谷歌上搜索 HTML 表单列表 但我在任何地方都找不到这种表单 谁能告诉我这个表单的确切名称以及它是否可以在 HTML 表单中使用 我只想在我的网站中添加这种形式 它适用于 HTML 还是我应
  • 将变量传递给 Google Cloud Functions

    我刚刚在 Beta Python 3 7 运行时中使用 HTTP 触发器编写了 Google Cloud Function 现在我试图弄清楚如何在调用函数时将字符串变量传递给函数 我已阅读文档 但没有找到任何相关内容 我的触发器类似于 ht
  • 如何在光线平行且不使用光线模式的情况下运行函数?

    After sudo pip3 install ray 我创建了一个函数foo 在射线装饰器中定义 import ray ray init ray remote def foo x print x 我希望能够使用foo并行和常规模式 忽略装
  • ViewModel 和 Service 类的实例化

    我试图理解 ViewModel 和 Service 类的实例化 并将其写下来供其他人使用 请在需要的地方更正 添加 ViewModel 和服务的实例化并不是以最常见的方式完成的 这是使用反射完成的 在 TipCalc 中 您有 public
  • 在特定日期触发 UILocalNotification

    我想开火UILocalNotification在特定日期 如果我使用这段代码 NSCalendar gregorian NSCalendar alloc initWithCalendarIdentifier NSGregorianCalen
  • 尝试理解 Pytorch 的 LSTM 实现

    我有一个包含 1000 个示例的数据集 其中每个示例都有5特征 a b c d e 我想喂7LSTM 的示例 以便它预测第 8 天的特征 a 阅读 nn LSTM 的 Pytorchs 文档 我得出以下结论 input size 5 hid
  • Akka 通过可堆叠行为拦截接收

    Akka 和 Scala 新手 请根据需要随意编辑问题 以便清楚地表达我在 Scala 和 Akka 领域的意图 在展示代码片段之前 这是我想要解决的问题 我本质上想开发一个通用模块 供我的团队在使用 Akka Actor 开发应用程序时使
  • Tcl正则表达式

    set d aa1 1 set d aa2 1 set d aa3 1 set d aa4 1 set d aa5 1 set d aa6 1 set d aa7 1 set d aa8 1 set d aa9 1 set d aa10 1
  • Fortran 读取混合文本和数字

    我正在使用 Fortran 90 读取包含以下格式数据的文件 number 125 var1 2 var2 1 var3 4 number 234 var1 3 var2 5 var3 1 我尝试了以下命令并且工作正常 read 2 tem
  • Ionic Jasmine:env.stopOnSpecFailure 编译成功后不是函数

    将 Ionic 与 jasmine karma 一起使用 在运行测试时 编译成功 但在 jasmine 仪表板中出现空屏幕 控制台中出现错误 以下教程 https leifwells github io 2017 08 27 testing
  • 受范数不等式约束的二次函数最小化

    我正在尝试解决以下不等式约束 给定 N 只股票的时间序列数据 我试图构建一个投资组合权重向量以最小化回报的方差 目标函数 min w T sum w s t e n T w 1 left w right leq C where w是权重向量
  • 从外部访问 Google 地图

    我有一些 javascript 代码 可以绘制 v3 GoogleMap 我想从外部访问地图对象 另一个js文件 是否有可能不创建额外的全局变量来引用地图 单独的 JS 文件在同一范围内加载和执行 因此无论您使用一个还是多个 JS 文件 都
  • 将参数从 tasklet 步骤添加到作业上下文,并在 Spring Batch 的后续步骤中使用

    目前 我使用 jobParameters 来获取 FlatFileItemReader 和 FlatFileItemWriter 的文件名 测试我的批次是可以的 但我的目标是读取某个目录中的文件 该目录中只有这个文件 并且文件名可能会改变
  • 错误:invalid_request 缺少必需参数:范围(Restify 和 Passportjs w/ Google OAuth2)

    因此 我在使用 Restify 和 Passportjs Google OAuth2 策略 时遇到了 Node js 应用程序的问题 当我使用passport authenticate 它给了我以下错误 400 这是一个错误 错误 无效 请
  • 提高Python代码的速度

    我有一些包含许多类的 python 代码 我用了cProfile发现程序运行总时间为68秒 我发现一个类中有以下函数Buyers这 68 秒中大约需要 60 秒 我必须运行该程序大约 100 次 因此速度的任何提高都会有所帮助 您能建议通过
  • 如何查找 C++ 中的内存泄漏

    在嵌入式环境中检测 C 内存泄漏的好方法是什么 我尝试重载 new 运算符来记录每个数据分配 但我一定做错了什么 这种方法不起作用 还有其他人遇到过类似的情况吗 这是 new 和 delete 运算符重载的代码 EDIT 完全披露 我正在寻
  • 带有大标题的导航栏 iOS 11 的图像

    AppStore app has an icon with an image on the right side of the NabBar with Large Title 如果有人的话我真的很感激知道如何实施 it or ideas关于