自定义AlertView的方法和改变Alert的弹出位置以及其宽度

此方法在IOS7中不适合

一、自定义AlertView

  1、首先新建一个OC类继承与AlertView。

  2、然后再.m中添加方法 - (void)layoutSubviews

    可以再这个方法里边改变alertView的背景图片,各个按钮的图片,等一系列的操作

    示例代码如下:

    

for (UIView *v inself.subviews) {

        if ([v isKindOfClass:[UIImageView class]]) {

            UIImageView *imageV = (UIImageView *)v;

            UIImage *image = [UIImage imageNamed:kAlertViewBackground];

            image = [[image stretchableImageWithLeftCapWidth:0topCapHeight:kAlertViewBackgroundCapHeight] retain];

            [imageV setImage:image];//替换alertView地背景图片

        }

        if ([v isKindOfClass:[UILabel class]]) {

            UILabel *label = (UILabel *)v;

            if ([label.text isEqualToString:self.title]) {

                label.font = [kAlertViewTitleFont retain];

                label.numberOfLines = 0;

                label.lineBreakMode = UILineBreakModeWordWrap;

                label.textColor = kAlertViewTitleTextColor;

                label.backgroundColor = [UIColor clearColor];

                label.textAlignment = UITextAlignmentCenter;

                label.shadowColor = kAlertViewTitleShadowColor;

                label.shadowOffset = kAlertViewTitleShadowOffset;//替换Title的样式

            }else{

                label.font = [kAlertViewMessageFont retain];

                label.numberOfLines = 0;

                label.lineBreakMode = UILineBreakModeWordWrap;

                label.textColor = kAlertViewMessageTextColor;

                label.backgroundColor = [UIColor clearColor];

                label.textAlignment = UITextAlignmentCenter;

                label.shadowColor = kAlertViewMessageShadowColor;

                label.shadowOffset = kAlertViewMessageShadowOffset;

            }

        }

        if ([v isKindOfClass:NSClassFromString(@"UIAlertButton")]) {//替换取消等按钮的样式

            UIButton *button = (UIButton *)v;

            UIImage *image = nil;

            if (button.tag == 1) {

                image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"gray"]];

            }else{

                image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"black"]];

            }

            image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width+1)>>1 topCapHeight:0];

            button.titleLabel.font = kAlertViewButtonFont;

            button.titleLabel.minimumFontSize = 10;

            button.titleLabel.textAlignment = UITextAlignmentCenter;

            button.titleLabel.shadowOffset = kAlertViewButtonShadowOffset;

            button.backgroundColor = [UIColor clearColor];

            [button setBackgroundImage:image forState:UIControlStateNormal];

            [button setTitleColor:kAlertViewButtonTextColorforState:UIControlStateNormal];

            [button setTitleShadowColor:kAlertViewButtonShadowColorforState:UIControlStateNormal];

        }

    }

二、更改AlertView的弹出位置及其宽度

  在代理方法

- (void)willPresentAlertView:(UIAlertView *)alertView

中修改。

原文地址:https://www.cnblogs.com/chenhaosuibi/p/3465850.html