Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - @available only for ios 9 and not ios 10

I am trying to see if this is possible. Looks like setting UICollectionViewFlowLayout's estimatedItemSize doesn't work very good in ios 9. It works perfectly fine ios 10. So i am thinking of implementing sizeForItemAt.. method only for ios 9. Is there anyway to do that using @available ?? Will be really helpful if someone can shed light.

like image 203
Kesava Avatar asked Nov 22 '16 11:11

Kesava


2 Answers

let systemVersion = UIDevice.currentDevice().systemVersion
println("iOS\(systemVersion)")

if systemVersion == 9.0 {
  //Do Something
}

You can use like below:

@available(iOS 10.0, *)
private func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    logParklee.verbose("willPresentNotification")
}

Or

if #available(iOS 10, *) {
    // use UIStackView
} else {
    // show sad face emoji
}

Or

guard #available(iOS 9, *) else {
    return
}

Or

@available(iOS 7, *)
func iOS7Work() {
    // do stuff

    if #available(iOS 8, *) {
        iOS8Work()
    }
}

@available(iOS 8, *)
func iOS8Work() {
    // do stuff
    if #available(iOS 9, *) {
        iOS9Work()
    }
}

@available(iOS 9, *)
func iOS9Work() {
    // do stuff
}
like image 119
Parth Adroja Avatar answered Nov 16 '22 03:11

Parth Adroja


I wanted to execute code when the version is lower iOS 11, here's how I did it:

if #available(iOS 11, *) {
  // This is probably empty
} else {
  // This code will only be executed if the version is < iOS 11
}

It's not the cleanest solution, but I couldn't find a better way and it does its job. 💡

like image 28
Jan Erik Schlorf Avatar answered Nov 16 '22 03:11

Jan Erik Schlorf