【问题标题】:Image not displaying in Simulator when setting it in the UINavigationController在 UINavigationController 中设置图像时,图像未显示在模拟器中
【发布时间】:2018-07-15 15:41:49
【问题描述】:

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

我正在尝试构建一个分配给我的故事板中的导航控制器的 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  

    }
}

【问题讨论】:

  • 这段代码是在setupView还是在哪里???
  • 你的图片尺寸是多少?

标签: ios swift cocoa-touch uinavigationcontroller


【解决方案1】:

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

将 navigationItem.titleView 设置为您在导航控制器显示的视图控制器上创建的 imageView,或者将 imageView 作为子视图添加到导航控制器的导航栏。

后者可以按如下方式进行:

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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 2011-02-16
    • 2021-04-23
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多