iOS Swift实现类似WhatsApp带自定义图片的UIAlertController SheetView
自定义带右侧图片的UIAlertController Sheet实现方案
原生UIAlertController没有提供直接添加自定义右侧图片的公开API,可通过遍历视图层级的方式实现需求,该方案兼容iOS13及以上系统版本。
实现注意事项
- 遍历视图层级时仅匹配UIAlertAction对应的布局视图,避免修改到系统内部其他组件
- 图片尺寸建议统一设置为24*24pt,适配系统默认行高不会出现布局错乱
- 自定义配置方法需要在
UIAlertController初始化完成、present之前调用,避免视图加载时序问题
Swift 实现代码
import UIKit extension UIAlertController { func addAction(title: String, style: UIAlertAction.Style, rightImage: UIImage?, handler: ((UIAlertAction) -> Void)? = nil) { let action = UIAlertAction(title: title, style: style, handler: handler) addAction(action) guard let image = rightImage else { return } // 遍历找到action对应的content视图 for subview in self.view.subviews { for case let actionView in subview.subviews where NSStringFromClass(type(of: actionView)).contains("ActionView") { if let label = actionView.subviews.first(where: { $0 is UILabel }) as? UILabel { // 设置文字左对齐 label.textAlignment = .left // 添加右侧自定义图片 let imageView = UIImageView(image: image) imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false actionView.addSubview(imageView) NSLayoutConstraint.activate([ imageView.trailingAnchor.constraint(equalTo: actionView.trailingAnchor, constant: -16), imageView.centerYAnchor.constraint(equalTo: actionView.centerYAnchor), imageView.widthAnchor.constraint(equalToConstant: 24), imageView.heightAnchor.constraint(equalToConstant: 24), label.leadingAnchor.constraint(equalTo: actionView.leadingAnchor, constant: 16) ]) } } } } } // 使用示例 let sheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) sheet.addAction(title: "选项1", style: .default, rightImage: UIImage(named: "icon1")) sheet.addAction(title: "选项2", style: .default, rightImage: UIImage(named: "icon2")) sheet.addAction(title: "取消", style: .cancel, rightImage: nil) present(sheet, animated: true)
Objective-C 实现代码
#import <UIKit/UIKit.h> @implementation UIAlertController (CustomRightImage) - (void)addActionWithTitle:(NSString *)title style:(UIAlertActionStyle)style rightImage:(UIImage *)image handler:(void (^)(UIAlertAction * _Nonnull))handler { UIAlertAction *action = [UIAlertAction actionWithTitle:title style:style handler:handler]; [self addAction:action]; if (!image) return; // 遍历获取Action对应的视图 for (UIView *subview in self.view.subviews) { for (UIView *actionView in subview.subviews) { if ([NSStringFromClass(actionView.class) containsString:@"ActionView"]) { UILabel *label = nil; for (UIView *v in actionView.subviews) { if ([v isKindOfClass:UILabel.class]) { label = (UILabel *)v; break; } } if (label) { label.textAlignment = NSTextAlignmentLeft; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.contentMode = UIViewContentModeScaleAspectFit; imageView.translatesAutoresizingMaskIntoConstraints = NO; [actionView addSubview:imageView]; [NSLayoutConstraint activateConstraints:@[ [imageView.trailingAnchor constraintEqualToAnchor:actionView.trailingAnchor constant:-16], [imageView.centerYAnchor constraintEqualToAnchor:actionView.centerYAnchor], [imageView.widthAnchor constraintEqualToConstant:24], [imageView.heightAnchor constraintEqualToConstant:24], [label.leadingAnchor constraintEqualToAnchor:actionView.leadingAnchor constant:16] ]]; } } } } } @end // 使用示例 UIAlertController *sheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [sheet addActionWithTitle:@"选项1" style:UIAlertActionStyleDefault rightImage:[UIImage imageNamed:@"icon1"] handler:nil]; [sheet addActionWithTitle:@"选项2" style:UIAlertActionStyleDefault rightImage:[UIImage imageNamed:@"icon2"] handler:nil]; [sheet addActionWithTitle:@"取消" style:UIAlertActionStyleCancel rightImage:nil handler:nil]; [self presentViewController:sheet animated:YES completion:nil];
以上代码实现的效果完全匹配你需要的文字左对齐、右侧显示自定义图片的ActionSheet需求。
内容的提问来源于stack exchange,提问作者Oscar Junius
相关产品推荐
相关产品推荐

