Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActionSheet in swift puts Cancel button top in iOS 7

I am in the process of rewriting my app from objective c to Swift and I noticed that UIActionSheet behaves differently in Swift version than in obj-c version.

Obj-c version

enter image description here

Swift version

enter image description here

This is a problem only on iOS 7, it works fine (meaning the cancel is on the bottom) on iOS 8 for both Swift and Obj-c versions

Here is relevant piece of code:

var sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil)
sheet.addButtonWithTitle("Camera")
sheet.addButtonWithTitle("Photo Library")
sheet.showInView(self.controller.view)

Any idea how to fix it?

like image 425
Lope Avatar asked Jan 23 '15 18:01

Lope


1 Answers

Turns out this was yet another case of finding answer right after asking the question.

All I had to do was add Cancel button as another button and then specify its index:

var sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
sheet.addButtonWithTitle("Camera")
sheet.addButtonWithTitle("Photo Library")
sheet.addButtonWithTitle("Cancel")
sheet.cancelButtonIndex = 2
sheet.showInView(self.controller.view)

Not sure if they changed the way how UIActionSheet is supposed to work in Swift or if it's bug that nobody cares to fix since it's deprecated in iOS 8 anyway

like image 108
Lope Avatar answered Oct 04 '22 02:10

Lope