问题描述
我正在努力使UIAlertController看起来像这样:
I am trying to make UIAlertController that looks like this:
我们如何定制UIAlertController以获得与此图片相同的结果?
How can we customize the UIAlertController to get the result something same as this picture ?
推荐答案
你想要的是什么do是一个popover,对于当前版本的iOS,你可以为iPad和iPhone实现相同的效果。
What you are trying to do is a popover, for current versions of iOS you can achieve the same effect for both iPad and iPhone.
1.-首先在Storyboard或xib上构建你的设计。然后引用它。
1.- Start by building your design on Storyboard or a xib. and then reference it.
2.-然后将其作为弹出窗口显示。
2.- then present it as a popover.
3.-也许你想要实现popoverdelegates旋转设备时避免错误的位置。
3.- maybe you will want to implement popoverdelegates to avoid wrong positions when rotating the device.
例如:
private static func presentCustomDialog(parent: UIViewController) -> Bool {
/// Loads your custom from its xib or from Storyboard
if let rateDialog = loadNibForRate() {
rateDialog.modalPresentationStyle = UIModalPresentationStyle.Popover
rateDialog.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
let x = parent.view.center
let sourceRectX : CGFloat
let maximumDim = max(UIScreen.mainScreen().bounds.height, UIScreen.mainScreen().bounds.width)
if maximumDim == 1024 { //iPad
sourceRectX = x.x
}else {
sourceRectX = 0
}
rateDialog.popoverPresentationController?.sourceView = parent.view
rateDialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
rateDialog.popoverPresentationController?.sourceRect = CGRectMake(sourceRectX, x.y, 0, 0)
rateDialog.popoverPresentationController?.popoverLayoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
rateDialog.popoverPresentationController?.delegate = parent
rateDialogParent = parent
dispatch_async(dispatch_get_main_queue(), {
parent.presentViewController(rateDialog, animated: true, completion: nil)
})
return true
}
return false
}
public class MyParentViewController: UIViewController, UIPopoverPresentationControllerDelegate {
/**
This function guarantees that the CustomDialog is always centered at parent, it locates the Dialog view
*/
public func popoverPresentationController(popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) {
let x = popoverPresentationController.presentingViewController.view.center
let newRect = CGRectMake(x.x, x.y, 0, 0)
rect.initialize(newRect)
}
}
这篇关于如何在swift ios中创建自定义UIAlertController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!