I am trying to make UIAlertController that looks like this:
How can we customize the UIAlertController to get the result something same as this picture ?
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.- Start by building your design on Storyboard or a xib. and then reference it.
2.- then present it as a popover.
3.- maybe you will want to implement popoverdelegates to avoid wrong positions when rotating the device.
for example:
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
}
Update: to achieve, point 3... on your parent
UIViewController
.
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)
}
}
I did the custom popup windows as @Hugo posted, after a while i found a library that is done in a very neat and magnificent way which can be used to implement custom popup views with less effort:
here is the link for the library on Github:
https://github.com/m1entus/MZFormSheetPresentationController
It is written in Objective c, as well as there is swift example included in the library's samples.
to include it into swift project you will need to use something called Bridging-header
来源:https://stackoverflow.com/questions/31997885/how-to-create-custom-uialertcontroller-in-swift-ios