想在UIAlertController或UIPopoverController Swift中显示UIPickerView?

iba*_*man 8 uipickerview uipopovercontroller ios swift uialertcontroller

我想在UIAlertController或UIPopoverController中显示UIPickerView,我是iOS新手,请在这里给我一些示例代码.

我搜索了他们但找不到任何满意的答案.我不想在UIActionSheet上这样做,因为它在iOS 9中已被弃用.我也希望它在Swift中而不是在Obj-C中.我尝试了下面的例子,但它不适合我.

http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/

Ren*_*iya 10

你的基本代码看起来像:

let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
alert.isModalInPopover = true

//  Create a frame (placeholder/wrapper) for the picker and then create the picker
let pickerFrame = CGRect(x: 17, y: 52, width: 270, height: 100) // CGRectMake(left), top, width, height) - left and top are like margins
let picker = UIPickerView(frame: pickerFrame)

//  set the pickers datasource and delegate
picker.delegate = self
picker.dataSource = self

//  Add the picker to the alert controller
alert.view.addSubview(picker)

//Do stuff and action on appropriate object.
Run Code Online (Sandbox Code Playgroud)

请查看以下答案.

有没有办法在Swift中将UIPickerView添加到UIAlertController(Alert或ActionSheet)中?


iba*_*man 2

下面的代码适用于显示 uipickerview 的 UIAertController。以前,当我在 iPhone 上进行更正时,它会在 iPad 上出现错误。它现在对两者都起作用。iPad 和 iPhone。

let alertView = UIAlertController(title: "Select Launguage", message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: UIAlertControllerStyle.ActionSheet);
    if !DeviceType.IS_IPAD{
   pickerView.center.x = self.view.center.x
    }
    alertView.view.addSubview(pickerView)
    if DeviceType.IS_IPAD{
        alertView.popoverPresentationController?.sourceView = self.view
        alertView.popoverPresentationController?.sourceRect = self.pickerView.bounds
    }
    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

    alertView.addAction(action)
    presentViewController(alertView, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

用于选择设备。代码如下:

enum UIUserInterfaceIdiom : Int
{
    case Unspecified
    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
    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
}
Run Code Online (Sandbox Code Playgroud)