[英]How to make an image visible only on iPhone 6/6s
因此,我想使用swift或Storyboard在xcode中使圖像僅在iPhone 6和6S上可見。 你們有什么建議嗎? :)
第一步是確定設備是6S還是6S。 這是執行此操作的Swift代碼:
import Foundation
import UIKit
extension UIApplication
{
public func isIPhone6or6S() -> Bool
{
let IPHONE_6 = "iPhone7,2"
let IPHONE_6S = "iPhone8,1"
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8 where value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}
return ( identifier == IPHONE_6 || identifier == IPHONE_6S) ? true : false;
}
}
然后,您可以在UIViewController easily
足夠UIViewController easily
使用它,如下所示:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var phoneOnlyImage: UIImageView!
@IBOutlet weak var hiddenStatusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// hide it if it is not an iPhone 6 or 6S
self.phoneOnlyImage.hidden = !(UIApplication.sharedApplication().isIPhone6or6S())
// show the label so you know if it's working
self.hiddenStatusLabel.text = (self.phoneOnlyImage.hidden) ? "Image is Hidden" : "Image is visible"
}
}
枚舉UIUserInterfaceIdiom:Int {case未指定案例Phone case Pad}
struct ScreenSize
{
static let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
static let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}
struct DeviceType
{
/* static let IS_IPHONE_4_OR_LESS = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
static let IS_IPHONE_5 = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0 */
static let IS_IPHONE_6 = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
靜態讓IS_IPHONE_6S = UIDevice.currentDevice()。userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
/* static let IS_IPHONE_6P = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
static let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
static let IS_IPAD_PRO = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0 */
}
if DeviceType.IS_IPHONE_6 {
//Enter Your Code Here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.