这是我的代码:
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可选值
尝试此代码以查看您的代码在哪里崩溃..它的起始点..然后解决在您的代码中navigationController
或Image
为零的那一部分...]
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")
}
您的控制器绝对不是导航控制器,如果它是您在应用程序中出现的第一个控制器,则必须在如下的willConnectTo函数下的场景委托中对其进行设置: