Xcode 5 & Asset Catalog: How to reference the LaunchImage?

前端 未结 14 1519
萌比男神i
萌比男神i 2020-11-28 18:17

I am using Xcode 5\'s Asset Catalog, and I would like to use my LaunchImage as the background image of my home view (a pretty common practice to make the transi

14条回答
  •  借酒劲吻你
    2020-11-28 18:47

    @codeman's answer updated for Swift 1.2:

    func splashImageForOrientation(orientation: UIInterfaceOrientation, size: CGSize) -> String? {
        var viewSize        = size
        var viewOrientation = "Portrait"
    
        if UIInterfaceOrientationIsLandscape(orientation) {
            viewSize        = CGSizeMake(size.height, size.width)
            viewOrientation = "Landscape"
        }
    
        if let imagesDict = NSBundle.mainBundle().infoDictionary as? [String: AnyObject] {
            if let imagesArray = imagesDict["UILaunchImages"] as? [[String: String]] {
                for dict in imagesArray {
                    if let sizeString = dict["UILaunchImageSize"], let imageOrientation = dict["UILaunchImageOrientation"] {
                        let imageSize = CGSizeFromString(sizeString)
                        if CGSizeEqualToSize(imageSize, viewSize) && viewOrientation == imageOrientation {
                            if let imageName = dict["UILaunchImageName"] {
                                return imageName
                            }
                        }
                    }
                }
            }
        }
    
        return nil
    
    }
    

    To call it, and to support rotation for iOS 8:

    override func viewWillAppear(animated: Bool) {
        if let img = splashImageForOrientation(UIApplication.sharedApplication().statusBarOrientation, size: self.view.bounds.size) {
            backgroundImage.image = UIImage(named: img)
        }
    }
    
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        let orientation = size.height > size.width ? UIInterfaceOrientation.Portrait : UIInterfaceOrientation.LandscapeLeft
    
        if let img = splashImageForOrientation(orientation, size: size) {
            backgroundImage.image = UIImage(named: img)
        }
    
    }
    

    Just what I needed, thanks!

提交回复
热议问题