UIAlertController带有方形(非圆角)角

时间:2015-08-31 22:17:19

标签: ios swift uialertcontroller

是否可以使UIAlertController具有方角而不是默认的圆角?

(我知道可以使用第三方库,但我想知道如何使用apple UIAlertController。)

3 个答案:

答案 0 :(得分:6)

简而言之,不,此时Apple并未提供已批准的 API方法,以便在特定情况之外设置UIAlertController样式。改变这种情况可能会导致拒绝,因为它没有遵守他们的指导原则:

  

UIAlertController类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

See here for reference

答案 1 :(得分:5)

实际上,您可以使用简单的遍历更改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)

目标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:

Square Alert View Controller

此代码在将来的iOS版本中不会崩溃,因为cornerRadius是每个UIView层的标准属性,并且您没有使用任何私有方法或属性来实现此目的。

答案 2 :(得分:3)

添加此行时,使警报角落正方形。 myAlert.view.backgroundColor = [UIColor whiteColor];

相关问题