[英]How to create Custom UIAlertController in swift ios?
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. 你要做的是弹出窗口,对于当前版本的iOS,你可以为iPad和iPhone实现同样的效果。
1.- Start by building your design on Storyboard or a xib. 1.-首先在Storyboard或xib上构建你的设计。 and then reference it. 然后引用它。
2.- then present it as a popover. 2.-然后把它作为一个弹出窗口。
3.- maybe you will want to implement popoverdelegates to avoid wrong positions when rotating the device. 3.-也许你会想要实现popoverdelegates以避免在旋转设备时出现错误的位置。
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
. 更新:在您的父UIViewController
上实现,指向3 ....
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: 我做了@Hugo发布的自定义弹出窗口,过了一段时间我找到了一个非常整洁和宏伟的方式完成的库,可以用来实现自定义弹出视图,省力:
here is the link for the library on Github: 这是Github上的库的链接:
https://github.com/m1entus/MZFormSheetPresentationController https://github.com/m1entus/MZFormSheetPresentationController
It is written in Objective c, as well as there is swift example included in the library's samples. 它是用Objective c编写的,并且库中的样本中包含了一些快速的例子。
to include it into swift project you will need to use something called Bridging-header 要将它包含在swift项目中,您需要使用一个名为Bridging-header的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.