根据背景图片设置导航栏大小

Set navigationbar size according to background image

我已经为导航栏设置了背景图片。这很好用。但我希望将导航栏高度调整为背景图像。目前背景图片的宽度也没有根据屏幕大小设置。

我尝试按照 here 所述设置导航栏的高度。这会显示一个更大的导航栏大约一秒钟,但随后会再次缩小到默认大小。

有谁知道如何实现我想要的吗?这是我想要实现的示例:image

Apple Documentation:

It is permissible to customize the appearance of the navigation bar using the methods and properties of the UINavigationBar class but you must never change its frame, bounds, or alpha values or modify its view hierarchy directly.

为了达到您标记的图像中看到的效果,他们很可能使用 collection 视图来布局他们的数据,并且该图像是 collection 视图的 header 的一部分.他们使导航栏的背景颜色清晰,但图像绝对不是导航栏本身的一部分。

Apple 建议永远不要手动更改导航栏的框架,因为它会扰乱其子视图和动画方法的布局代码。

您可以将导航栏子类化并尝试创建类似的东西,或者走更简单的路线并使导航栏清晰(UIColor(white: 0, alpha: 1) 而不是 .clear 否则它可能显示不正确)和让底层视图显示图像(e.x。collection 视图 header 延伸到视图控制器的顶部)。

这将允许您自由调整图像的高度和宽度,而无需子类化导航栏,也不会产生潜在的错误。

您可以自定义 NavigationBar class。
它可以帮助你
https://developer.apple.com/library/archive/samplecode/NavBar/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007418-Intro-DontLinkElementID_2