Ian
Ian

Reputation: 7558

UIActionsheet not displaying all buttons on iPads running iOS7

Instances of UIActionSheet don't display correctly on iPads running iOS7. For some reason, they display the cancel button and leave off the last button. The same code works fine on iOS8.

You would expect the cancel button to be ignored, given that tapping elsewhere on the screen will close the action sheet. Does anyone know why this is the case?

Exactly the same code is used in both cases:

// Create UIActionSheet    
let mapOptions = UIActionSheet(title: "Select map type", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Standard", "Hybrid", "Satellite")

// Display from bar button
mapOptions.showFromBarButtonItem(self.mapTypeButton, animated: true)

comparison of UIActionSheet instances on iOS 7 and 8

Upvotes: 4

Views: 912

Answers (2)

ethanchli
ethanchli

Reputation: 296

I'm facing the same problem with Swift. Your answer saved me. I found you can still use this init method (with cancel button nil) to make your code more succinct.

let mapOptions = UIActionSheet(title: "Select map type", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil, otherButtonTitles: "Standard", "Hybrid", "Satellite")

and then with the line you provided

mapOptions.cancelButtonIndex = mapOptions.addButtonWithTitle("Cancel")

(I don't have enough reputation to add a comment so write here instead.)

Upvotes: 3

Ian
Ian

Reputation: 7558

I was able to solve the problem by avoiding the default UIActionSheet constructor and adding buttons individually. This - and making sure the cancel button is added last - resolves the issue.

// Create the UIActionSheet
var mapOptions = UIActionSheet()
mapOptions.title = "Select map type"
mapOptions.addButtonWithTitle("Standard")
mapOptions.addButtonWithTitle("Hybrid")
mapOptions.addButtonWithTitle("Satellite")

// Add a cancel button, and set the button index at the same time
mapOptions.cancelButtonIndex = mapOptions.addButtonWithTitle("Cancel")
mapOptions.delegate = self

// Display the action sheet
mapOptions.showFromBarButtonItem(self.mapTypeButton, animated: true)

Upvotes: 5

Related Questions