有没有可能让UIAlertController
有方角而不是默认的圆角?
(我知道使用第三方库是可行的,但我想知道如何使用苹果UIAlertController
来实现。)
发布于 2017-02-12 06:28:47
实际上,您可以使用简单的遍历来更改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
在实现中包含头部并使用以下内容:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController.view traverseRadius:0];
[self presentViewController:alertController animated:YES completion:nil];
你会得到完美的正方形UIAlertController:
这段代码在未来的iOS版本中不会崩溃,因为cornerRadius是每个UIView层的标准属性,您不会使用任何私有方法或属性来实现这一点。
发布于 2015-09-16 18:04:35
添加此行时,将警报角设置为方形。myAlert.view.backgroundColor = UIColor whiteColor;
https://stackoverflow.com/questions/32320779
复制相似问题