需求:
自定义了无数的弹窗,发现系统自带的有许多私有属性,随扩展玩玩,实现以下三种效果:(核心是通过私有 api 属性 通过 KVC 实现;)
效果一:弹窗图片
@objc func showActionSheet4(){
let message = """
/// 我试图解决UIAlertController的局限性。
""";
let contentView = UIImageView(image: UIImage(named: "Skull.jpg"))
contentView.contentMode = .scaleAspectFit
UIAlertController(title: "Select date", message: message, preferredStyle: .actionSheet)
.addActionTitles()
.setContent(view: contentView, height: 300)
.present()
}
效果二:弹窗嵌套导航
@objc func showAlertContentVC() {
let message = "弹窗内嵌套了一个导航控制器";
UIAlertController(title: "嵌套导航", message: message, preferredStyle: .actionSheet)
.addActionTitles([kTitleCancell])
.setContent(vc: UINavigationController(rootViewController: ThirdViewController()), height: 300)
.present()
}
效果三:弹窗自定义UIAlertAction
@objc func showActionSheet7(){
var list = Array.init(count: 20) { "item_\($0)" }
list.append(kTitleCancell)
let alertVC = UIAlertController(title: "请选择", message: "UIAlertAction 自定义 ", preferredStyle: preferredStyle)
list.map { e in
if e == kTitleCancell {
let action = UIAlertAction(title: e, style: .cancel, handler: { action in
DDLog(action.title)
})
return action
}
let action = UIAlertAction(title: "", style: .default, handler: nil)
let contentView = NNAlertActionView()
contentView.imageView.image = UIImage(named: "Skull.jpg")
contentView.textLabel.text = e
contentView.detailTextLabel.text = "detailTextLabel"
contentView.addGestureTap { reco in
action.setValue(NSNumber(booleanLiteral: true), forKey: "checked")
alertVC.dismiss(animated: true, completion: nil)
DDLog(e)
}
action.setContent(view: contentView, inset: .zero)
return action
}.forEach {
alertVC.addAction($0)
}
alertVC.present()
}
UIAlertController 私有 api 列表及其类型:
_message, @"NSString"
_attributedTitle, @"NSAttributedString"
_attributedMessage, @"NSAttributedString"
_attributedDetailMessage, @"NSAttributedString"
_linkedAlertControllers, @"NSSet"
_cancelAction, @"UIAlertAction"
_actionToKeyCommandsDictionary,
_keyCommandToActionMapTable, @"NSMapTable"
_resolvedStyle, q
_preferredStyle, q
_contentViewController, @"UIViewController"
_textFieldViewController,
_backButtonDismissGestureRecognizer,
_ownedTransitioningDelegate, @
_addContentViewControllerToViewHierarchyNeeded, B
_isInSupportedInterfaceOrientations, B
_isInRecomputePreferredContentSize, B
_batchActionChangesInProgressCount, q
_presenter, @"_UIAlertControllerShimPresenter"
_actionsWithInvokedHandlers, @"NSPointerArray"
_alertControllerStackManager,
_hidden, B
_springLoaded, B
__shouldFlipFrameForShimDismissal, B
__shouldAllowNilParameters, B
_hasPreservedInputViews, B
_actions, @"NSMutableArray"
_headerContentViewController, @"UIViewController"
_separatedHeaderContentViewController,
_styleProvider,
_preferredAction, @"UIAlertAction"
_temporaryAnimationCoordinator,
_previewInteractionController,
__visualStyle, @"UIAlertControllerVisualStyle"
_indexesOfActionSectionSeparators, @"NSIndexSet"
__actionDelimiterIndices, @"NSMutableArray"
__compatibilityPopoverController,
__systemProvidedPresentationView, @"UIView"
__systemProvidedPresentationDelegate,
_systemProvidedGestureRecognizer,
_coordinatedActionPerformingDelegate,
__presentationSourceRepresentationView, @"UIView"
_titleMaximumLineCount, q
_titleLineBreakMode, q
UIAlertAction 私有 api 列表及其类型:
_title, @"NSString"
_titleTextAlignment, q
_enabled, B
_checked, B
_isPreferred, B
_imageTintColor, @"UIColor"
_titleTextColor, @"UIColor"
_style, q
_handler, @?
_simpleHandler, @?
_image, @"UIImage"
_shouldDismissHandler, @?
__descriptiveText, @"NSString"
_contentViewController, @"UIViewController"
_keyCommandInput, @"NSString"
_keyCommandModifierFlags, q
__representer,
__interfaceActionRepresentation, @"UIInterfaceAction"
__alertController, @"UIAlertController"
github