gpt4 book ai didi

ios - 在 UINavigationController 中设置图像时,图像未显示在模拟器中

转载 作者:行者123 更新时间:2023-11-30 11:47:56 26 4
gpt4 key购买 nike

我正在尝试构建一个分配给 Storyboard中的导航 Controller 的 Cocoa Touch 类,以在中心的导航栏中显示图像。应用程序已构建,但图像未显示,我可以找出原因。

我正在尝试构建一个分配给 Storyboard中的导航 Controller 的 Cocoa Touch 类,以在中心的导航栏中显示图像。应用程序已构建,但图像未显示,我可以找出原因。

class NavBarImg: UINavigationController {
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)!
setupView()
}

func setupView()
{
if navigationController == nil{
return
}

let image = #imageLiteral(resourceName: "BarTabsNavLogoWhite")
let imageView = UIImageView(image: image)

let bannerWidth = navigationBar.frame.size.width
let bannerHeight = navigationBar.frame.size.height

let bannerX = bannerWidth / 2 - image.size.width / 2
let bannerY = bannerHeight / 2 - image.size.height / 2

imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit

self.navigationItem.titleView = imageView

}
}

最佳答案

导航 Controller 不会显示自己的导航项,因此仅将 self.navigationItem.titleView 设置为您创建的 imageView 就不会显示图像。

将 navigationItem.titleView 设置为您在 View Controller 上创建的由导航 Controller 显示的 imageView,或者将 imageView 作为 subview 添加到导航 Controller 的导航栏。

后者可以按如下方式完成:

class CustomNavigationController: UINavigationController {

override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBar()
}

func setupNavigationBar() {
let image = #imageLiteral(resourceName: "BarTabsNavLogoWhite")
let imageView = UIImageView(image: image)
imageView.frame = navigationBar.frame
navigationBar.addSubview(imageView)
}
}

关于ios - 在 UINavigationController 中设置图像时,图像未显示在模拟器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48628412/

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