设置导航栏背景图像

问题描述 投票:1回答:2

这是我的代码:

let logo = UIImage(named: "navigationbar")
self.navigationController!.navigationBar.setBackgroundImage(logo!.resizableImage(withCapInsets: UIEdgeInsets(top: 0,left: 0, bottom: 0 ,right: 0), resizingMode: .stretch), for: .default)

但是不起作用。这是错误代码。

线程1:致命错误:展开包装时意外发现nil可选值

swift swift4 navigationbar
2个回答
0
投票

尝试此代码以查看您的代码在哪里崩溃..它的起始点..然后解决在您的代码中navigationControllerImage为零的那一部分...]

if let logo = UIImage(named: "navigationbar") {
    if let navigation = self.navigationController {

       navigation.navigationBar.setBackgroundImage(logo!.resizableImage(withCapInsets: UIEdgeInsets(top: 0,left: 0, bottom: 0 ,right: 0), resizingMode: .stretch), for: .default)
      }else {
        print("Navigation cotroller not found and its nil")
      }
} else {
   print("problem wiyh image file")
}

1
投票

您的控制器绝对不是导航控制器,如果它是您在应用程序中出现的第一个控制器,则必须在如下的willConnectTo函数下的场景委托中对其进行设置:

© www.soinside.com 2019 - 2024. All rights reserved.