带有方角(非圆角)的 UIAlertController
UIAlertController with square (not rounded) corners
是否可以让 UIAlertController
有方角而不是默认的圆角?
(我知道可以使用第三方库,但我想知道如何使用苹果 UIAlertController
。)
简而言之,不,目前 Apple 不提供 approved API 方法来在给定情况之外设置您的 UIAlertController
样式。更改它可能会导致拒绝,因为它不符合他们的指导方针:
The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
添加此行时,使警告角变为正方形。 myAlert.view.backgroundColor = [UIColor whiteColor];
其实你可以通过简单的遍历来改变cornerRadius:
extension UIView {
func traverseRadius(_ radius: Float) {
layer.cornerRadius = CGFloat(radius)
for subview: UIView in subviews {
subview.traverseRadius(radius)
}
}
}
然后您可以向警报控制器显示:
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alertController.view.traverseRadius(0)
self.present(alertController, animated: true, completion: nil)
Objective C 中的相同内容:
// UIView+TraverseRadius.h
#import <UIKit/UIKit.h>
@interface UIView (TraverseRadius)
- (void)traverseRadius:(CGFloat)radius;
@end
// UIView+TraverseRadius.m
#import "UIView+TraverseRadius.h"
@implementation UIView (TraverseRadius)
- (void)traverseRadius:(CGFloat)radius {
[self.layer setValue:@(radius) forKey:@"cornerRadius"];
for (UIView *subview in self.subviews) {
[subview traverseRadius:radius];
}
}
@end
包括 header 并在实现中使用以下内容:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController.view traverseRadius:0];
[self presentViewController:alertController animated:YES completion:nil];
你会得到完美的正方形 UIAlertController:
此代码在未来的 iOS 版本中不会崩溃,因为 cornerRadius 是每个 UIView 层的标准 属性 并且您没有使用任何私有方法或属性来实现此目的。
是否可以让 UIAlertController
有方角而不是默认的圆角?
(我知道可以使用第三方库,但我想知道如何使用苹果 UIAlertController
。)
简而言之,不,目前 Apple 不提供 approved API 方法来在给定情况之外设置您的 UIAlertController
样式。更改它可能会导致拒绝,因为它不符合他们的指导方针:
The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
添加此行时,使警告角变为正方形。 myAlert.view.backgroundColor = [UIColor whiteColor];
其实你可以通过简单的遍历来改变cornerRadius:
extension UIView {
func traverseRadius(_ radius: Float) {
layer.cornerRadius = CGFloat(radius)
for subview: UIView in subviews {
subview.traverseRadius(radius)
}
}
}
然后您可以向警报控制器显示:
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alertController.view.traverseRadius(0)
self.present(alertController, animated: true, completion: nil)
Objective C 中的相同内容:
// UIView+TraverseRadius.h
#import <UIKit/UIKit.h>
@interface UIView (TraverseRadius)
- (void)traverseRadius:(CGFloat)radius;
@end
// UIView+TraverseRadius.m
#import "UIView+TraverseRadius.h"
@implementation UIView (TraverseRadius)
- (void)traverseRadius:(CGFloat)radius {
[self.layer setValue:@(radius) forKey:@"cornerRadius"];
for (UIView *subview in self.subviews) {
[subview traverseRadius:radius];
}
}
@end
包括 header 并在实现中使用以下内容:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController.view traverseRadius:0];
[self presentViewController:alertController animated:YES completion:nil];
你会得到完美的正方形 UIAlertController:
此代码在未来的 iOS 版本中不会崩溃,因为 cornerRadius 是每个 UIView 层的标准 属性 并且您没有使用任何私有方法或属性来实现此目的。