zoukankan      html  css  js  c++  java
  • 自定义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

    中修改。

  • 相关阅读:
    VirtualBox COM对象获取失败
    layui的表单功能
    phpstudy+phpstorm配置xdebug
    wamp2.5怎么设置虚拟域名
    腾讯微博-转播到微博的简单使用
    新浪微博--分享到微博的简单使用
    CKEdiotr使用入门
    GridView删除行
    Python迭代器笔记
    Java基础之打印万年历
  • 原文地址:https://www.cnblogs.com/chenhaosuibi/p/3465850.html
Copyright © 2011-2022 走看看