How to specify size for iPhone 6/7 customised edge-to-edge image?

后端 未结 12 992
死守一世寂寞
死守一世寂寞 2020-11-28 01:59

Say I want a bundled image to take up all available screen width in an iPhone app - for example a banner. I\'d create my_banner.png with width 320px

12条回答
  •  旧巷少年郎
    2020-11-28 02:07

    Please try this class to change the image name programmatically.

    import UIKit
    
    class AGTools: NSObject {
        class func fullWidthImage(imageName: String!) -> String!{
            let screenWidth = UIScreen.mainScreen().bounds.size.width
            switch (screenWidth){
            case 320:
                // scale 2x or 1x
                return (UIScreen.mainScreen().scale > 1.0) ? "\(imageName)@2x" : imageName
            case 375:
                return "\(imageName)-375w@2x"
            case 414:
                return "\(imageName)-414w@3x"
            default:
                return imageName
            }
        }
    }
    

    use this method like this.

    _imgTest.image = UIImage(named: AGTools.fullWidthImage("imageName"))
    

提交回复
热议问题