UIAlertController

博客探讨了如何尝试自定义UIAlertController界面,由于苹果未提供官方方法,作者通过 runtime 打印属性并利用 KVC 访问私有属性进行有限的定制,如设置按钮的左右图片和状态。文章介绍了 UIAlertController 的基本用法,包括创建、添加按钮和输入框,并展示了如何利用私有属性 _image 为 UIAlertAction 设置图片。此外,还列出了 UIAction 和 UIAlertController 的部分私有属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本来想研究下UIAlertController怎么能自定义界面,结果苹果没有提供相应的方法,没有办法进行自定制,结果从网上找出的一些方法都是都不好用,于是想到用私有的属性设置,于是使用runtime打印出alertController和alertAction的属性,然后用利用kvc进行私有属性的设置,发现也只能很简单的进行简单的定制。比如 action设置左边的图片,右边设置打钩的状态,这些私有属性都贴在文章后面,下面简单写点alertController 的用法。

+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

上述方法为创建方法。

分别传title(标题),massage(详细信息),preferredStyle(弹出风格)

每个按钮都为自己创建UIAlertAction实例,以下是创建方法。

  1. + (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;

title(按钮标题),style(风格定义如下),handler(按钮的点击事件,是一个代码块)

  1. typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
  2.    UIAlertActionStyleDefault = 0,
  3.    UIAlertActionStyleCancel,
  4.    UIAlertActionStyleDestructive
  5. } NS_ENUM_AVAILABLE_IOS(8_0);

创建好action之后添加到alertController中,以下方法添加:

  1. - (void)addAction:(UIAlertAction *)action;

如果想在alertController中添加输入框,用以下方法:

  1. - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;

configurationHandler是一个代码块,用于textField的初始化工作。

以下属性可以获得alertController中所有输入框实例,以数组的形式返回:

  1. @property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;



以下说的才是重点:

给每个action设置图片,系统没有提供方法,但是可以用KVC的方式进行设置

  1. UIImage *image = [UIImage imageNamed:@"Untitled.jpg"];
  2. image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  3. [action setValue:image forKey:@"_image"];

_image是UIAction中的一个私有属性,是对action进行图片设置的。

其实UIAction还有很多的私有属性,如下:

  1. _title    设置文字的
  2. _titleTextAlignment    设置文字的对齐方式
  3. _enabled  设置是否可用
  4. _checked  右方打钩
  5. _isPreferred
  6. _imageTintColor
  7. _titleTextColor  设置文字颜色
  8. _style
  9. _handler
  10. _simpleHandler
  11. _image   设置图片
  12. _shouldDismissHandler
  13. __descriptiveText
  14. _contentViewController
  15. _keyCommandInput
  16. _keyCommandModifierFlags
  17. __representer
  18. __alertController

以下是UIAlertViewController的私有属性:

  1. _message
  2. _attributedTitle
  3. _attributedMessage
  4. _attributedDetailMessage
  5. _actionDelimiterIndices
  6. _linkedAlertControllers
  7. _cancelAction
  8. _actionToKeyCommandsDictionary
  9. _keyCommandToActionMapTable
  10. _resolvedStyle
  11. _preferredStyle
  12. _styleProvider
  13. _contentViewController
  14. _textFieldViewController
  15. _backButtonDismissGestureRecognizer
  16. _selectGestureRecognizer
  17. _ownedTransitioningDelegate
  18. _shouldInformViewOfAddedContentViewController
  19. _isInSupportedInterfaceOrientations
  20. _shouldEnsureContentControllerViewIsVisibleOnAppearance
  21. _hidden
  22. _ignoresKeyboardForPositioning
  23. __shouldAllowNilParameters
  24. _hasPreservedInputViews
  25. __shouldFlipFrameForShimDismissal
  26. _actions
  27. _preferredAction
  28. __presentationSourceRepresentationView
  29. __visualStyle
  30. __compatibilityPopoverController
  31. __systemProvidedPresentationView
  32. __systemProvidedPresentationDelegate
  33. _systemProvidedGestureRecognizer
  34. _temporaryAnimationCoordinator
  35. _previewInteractionController


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值