Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置导航栏背景图像_Ios_Swift_Swift4_Navigationbar - Fatal编程技术网

Ios 设置导航栏背景图像

Ios 设置导航栏背景图像,ios,swift,swift4,navigationbar,Ios,Swift,Swift4,Navigationbar,这是我的代码: 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) 但它不起作用。这是错误

这是我的代码:

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

您的控制器绝对不是导航控制器,如果它是您在应用程序中显示的第一个控制器,则必须在WillConnector下的场景代理中将其设置为如下功能:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: windowScene)
    window?.makeKeyAndVisible()
    let controller = UINavigationController(rootViewController: ViewController())
    window?.rootViewController = controller
}
@objc fileprivate func callNavigationController() {
let controller = UINavigationController(rootViewController: YourController())
controller.modalPresentationStyle = .fullScreen
present(controller, animated: true, completion: nil)
}
如果您使用按钮操作集函数调用控制器,该函数显示如下导航控制器:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let windowScene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: windowScene)
    window?.makeKeyAndVisible()
    let controller = UINavigationController(rootViewController: ViewController())
    window?.rootViewController = controller
}
@objc fileprivate func callNavigationController() {
let controller = UINavigationController(rootViewController: YourController())
controller.modalPresentationStyle = .fullScreen
present(controller, animated: true, completion: nil)
}
现在在viewDidLoad中设置导航栏背景, 第一个使用guard let语句展开图像:

guard let logo = UIImage(named: "navigationbar") else { return }
设置导航栏背景图像后:

navigationController?.navigationBar.setBackgroundImage(logo.resizableImage(withCapInsets: UIEdgeInsets(top: 0,left: 0, bottom: 0 ,right: 0), resizingMode: .stretch), for: .default)

就这些

哦,太酷了。打印“未找到导航滚轮及其零”。我是初学者:((现在你需要检查为什么为零,因为你需要发布代码…你在哪里添加它?我在viewDidLoad()函数中添加了你的代码。但我不知道如何检查为什么为零。我应该导入Navigationbar吗?你的控制器在navigation controller中?