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

    中修改。

  • 相关阅读:
    Git 分支创建,合并, 分支切换, 分支拉取,提交
    Win7 Nodejs 安装
    .ssh github
    xxxx.IronManager was loaded by com.taobao.pandora.boot.loader.XxxxClassLoader@xxx,it should be loaded by Pandora Container...与摒弃引进别的项目的一些冲突包
    推荐一波微软家的浏览器:EDGE
    谷歌浏览器新功能 Copy Declaration
    微信支付回调数据接收不完整解决方案
    开源物联网框架EasyIot(适用于快递柜&售货机)
    开源物联网框架EasyIot场景落地(适用于快递柜、储物柜)
    海康摄像头音频方案(播放音频文件+语音对讲+语音转发)支持window/Linuxjava版本
  • 原文地址:https://www.cnblogs.com/chenhaosuibi/p/3465850.html
Copyright © 2011-2022 走看看