iOS開(kāi)發(fā)-自定義彈窗(多種樣式若厚,仿系統(tǒng),可拿去修改成自己想要的樣子)

前言:

公司項(xiàng)目彈窗的地方比較多蜒什,彈窗的樣式也有很多種测秸,所以就根據(jù)項(xiàng)目里的多種彈窗樣式,整合成一個(gè)可重復(fù)利用的全局彈窗灾常,可自己手動(dòng)再次自定義霎冯。

有多種樣式:最簡(jiǎn)單的文字提示彈窗(有標(biāo)題、詳細(xì)信息钞瀑,還有確定沈撞、取消按鈕,這些皆可單獨(dú)存在)雕什,首頁(yè)一張大廣告圖片彈窗缠俺,有單選显晶、多選(列表多選)、勾選彈窗壹士,輸入框彈窗(可以自己修改為幾個(gè)輸入框都行)磷雇,TextView彈窗,還有圖片和文字結(jié)合的說(shuō)明彈窗躏救,還有自定義彈窗(可以自行添加想要添加到彈窗上的視圖)唯笙;彈窗右上角有關(guān)閉按鈕,可以自行選擇是否展示盒使,等等功能自行探索.....

具體可以自己嘗試崩掘,先說(shuō)一下最簡(jiǎn)單的文字提示彈窗,如下圖所示:


0D2259AD-FEAB-4F23-A50D-473C128987C4.png

部分具體實(shí)現(xiàn)如下:
PromptView文件:

- (nonnull instancetype)initWithTitle:(id)title
                              message:(id)message
                            imageName:(NSString *)imageName
                       preferredStyle:(PromptViewStyle)preferredStyle {
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        ///dissmiss監(jiān)聽(tīng)
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dimiss) name:DissmissPromptViewNotificationKey object:nil];
        
        self.backgroundColor = [UIColor clearColor];
        self.style = preferredStyle;
        self.maxLength = ULONG_MAX;
        self.titleLabelHasClickableText = NO;
        self.tapBackgroundHide = YES;
        self.isFilterWihtespace = YES;
        self.isAllowInputWihtespace = YES;
        self.spacingBetweenCell = 0;
        
        CGFloat contentWidth = FIT_LENGTH_PT(305);
        self.contentHeight = TopSpace + BottomSpace;
        CGFloat contentHeight = 0;
        
        WS(weakSelf);
        
        UIView *contentView = [[UIView alloc] init];
//        [self.contentView addGestureRecognizer:tap];
        contentView.backgroundColor = [UIColor whiteColor];
        contentView.layer.cornerRadius = FIT_LENGTH_PT(15);
        [self addSubview:contentView];
        self.contentView = contentView;
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(contentWidth);
            make.height.mas_equalTo(self.contentHeight);
            make.center.equalTo(weakSelf).offset(0);
        }];
}
- (void)addTitleLabelWithTitle:(id)title {
    WS(weakSelf);
    CGFloat textLeftRightSpace = FIT_LENGTH_PT(40);
    UILabel *titleLabel = nil;
    if (![CommonUtils isEmptyString:title]||![CommonUtils isEmptyAttributedString:title]) {
        titleLabel = [[UILabel alloc] init];
        titleLabel.userInteractionEnabled = YES;
        titleLabel.font = [UIFont systemFontOfSize:FIT_LENGTH_PT(16)];
        titleLabel.textColor = kProjectTextColor;
        titleLabel.numberOfLines = 0;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        NSAttributedString * attrText = [title isKindOfClass:[NSString class]]?[self attributedStringWithLabel:titleLabel text:title lineSpacing:5]:title;
        if ([attrText isKindOfClass:[NSMutableAttributedString class]]) {
            NSMutableAttributedString *mAttr = (NSMutableAttributedString *)attrText;
            mAttr.lineSpacing = 5;
        }
        titleLabel.attributedText = attrText;
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).offset(textLeftRightSpace);
            make.right.equalTo(weakSelf.contentView).offset(-textLeftRightSpace);
            if (self.style == PromptViewStyleImageText) {
                make.top.equalTo(weakSelf.imageView.mas_bottom).offset(14);
            } else {
                make.top.equalTo(weakSelf.contentView).offset(TopSpace);
            }
        }];
        [self.contentView layoutIfNeeded];
        CGFloat titleTextHeight = titleLabel.size.height;
        CGFloat contentHeight = 0;
        if (self.style == PromptViewStyleImageText) {
            contentHeight += 14;
        }
        contentHeight += titleTextHeight;
        [self updateContentHeight:contentHeight];
    }
}
- (void)addMessageLabelWithMessage:(id)message {
    WS(weakSelf);
    CGFloat textLeftRightSpace = FIT_LENGTH_PT(40);
    if (![CommonUtils isEmptyString:message]||![CommonUtils isEmptyAttributedString:message]) {
        UILabel *messageLabel = [[UILabel alloc] init];
        messageLabel.font = [UIFont systemFontOfSize:FIT_LENGTH_PT(15)];
        messageLabel.textColor = kProjectLightTextColor;
        messageLabel.numberOfLines = 0;
        messageLabel.textAlignment = NSTextAlignmentCenter;
        NSAttributedString * attrText = [message isKindOfClass:[NSString class]]?[self attributedStringWithLabel:messageLabel text:message lineSpacing:5]:message;
        if ([attrText isKindOfClass:[NSMutableAttributedString class]]) {
            NSMutableAttributedString *mAttr = (NSMutableAttributedString *)attrText;
            mAttr.lineSpacing = 5;
        }
        messageLabel.attributedText = attrText;
        [self.contentView addSubview:messageLabel];
        self.messageLabel = messageLabel;
        [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).offset(textLeftRightSpace);
            make.right.equalTo(weakSelf.contentView).offset(-textLeftRightSpace);
            if ([CommonUtils isNullObject:weakSelf.titleLabel]) {
                if (self.style == PromptViewStyleImageText) {
                    make.top.equalTo(weakSelf.imageView.mas_bottom).offset(8);
                } else {
                    make.top.equalTo(weakSelf.contentView).offset(TopSpace);
                }
            } else {
                make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(5);
            }
        }];
        [self.contentView layoutIfNeeded];
        CGFloat messageTextHeight = messageLabel.size.height;
        CGFloat contentHeight = 0;
        contentHeight += (messageTextHeight + 5);
        [self updateContentHeight:contentHeight];
    }
}

PromptViewAction文件:

@interface PromptViewAction ()
@property(nonatomic, copy, readwrite) NSString *title;
@property(nonatomic, assign, readwrite) PromptViewActionStyle style;
@property(nonatomic, copy) void (^handler)(PromptViewAction * _Nonnull action);
@end

@implementation PromptViewAction

+ (instancetype)actionWithTitle:(NSString *)title style:(PromptViewActionStyle)style handler:(void (^)(PromptViewAction * _Nonnull))handler {
    PromptViewAction *action = [[self alloc] init];
    action.title = title;
    action.style = style;
    action.handler = handler;
    return action;
}

- (nonnull instancetype)init {
    self = [super init];
    if (self) {
        self.tapHide = YES;
        _button = [[UIButton alloc] init];
        [self.button setBackgroundImage:[UIImage imageWithColor:KProjectF6F8F9GrayBackGroundColor] forState:UIControlStateDisabled];
        [self.button setTitleColor:kProjectLightTextColor forState:UIControlStateDisabled];
        self.button.titleLabel.font = [UIFont systemFontOfSize:FIT_LENGTH_PT(17)];
        self.button.layer.cornerRadius = 41/2.0;
        self.button.layer.masksToBounds = YES;
        [self.button addTarget:self action:@selector(handleAlertActionEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)handleAlertActionEvent:(id)sender {
    if (self.isTapHide) {
        [[NSNotificationCenter defaultCenter] postNotificationName:DissmissPromptViewNotificationKey object:nil];
    }
    if (self.handler) {
        self.handler(self);
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    [self.button setTitle:title forState:UIControlStateNormal];
}

- (void)setStyle:(PromptViewActionStyle)style {
    _style = style;
    if (style == PromptViewActionStyleDefault) {
        [self.button setBackgroundImage:[UIImage imageWithColor:kProjectColorNewGreen] forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else if (style == PromptViewActionStyleCancel) {
        [self.button setBackgroundImage:[UIImage imageWithColor:KProjectF6F8F9GrayBackGroundColor] forState:UIControlStateNormal];
        [self.button setTitleColor:kProjectLightTextColor forState:UIControlStateNormal];
    } else {
        [self.button setBackgroundImage:[UIImage imageWithColor:kProjectButtonRedColor] forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}

- (void)setEnabled:(BOOL)enabled {
    self.button.enabled = enabled;
}

- (void)setTapHide:(BOOL)tapHide {
    _tapHide = tapHide;
}

@end

具體代碼看這里

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末少办,一起剝皮案震驚了整個(gè)濱河市苞慢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌英妓,老刑警劉巖枉疼,帶你破解...
    沈念sama閱讀 211,743評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異鞋拟,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)惹资,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)贺纲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人褪测,你說(shuō)我怎么就攤上這事猴誊。” “怎么了侮措?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,285評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵懈叹,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我分扎,道長(zhǎng)澄成,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,485評(píng)論 1 283
  • 正文 為了忘掉前任畏吓,我火速辦了婚禮墨状,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘菲饼。我一直安慰自己肾砂,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布宏悦。 她就那樣靜靜地躺著镐确,像睡著了一般包吝。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上源葫,一...
    開(kāi)封第一講書(shū)人閱讀 49,821評(píng)論 1 290
  • 那天诗越,我揣著相機(jī)與錄音,去河邊找鬼臼氨。 笑死景馁,一個(gè)胖子當(dāng)著我的面吹牛介衔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 38,960評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼误甚,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了谒亦?” 一聲冷哼從身側(cè)響起彬向,我...
    開(kāi)封第一講書(shū)人閱讀 37,719評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎屡拨,沒(méi)想到半個(gè)月后只酥,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,186評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡呀狼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評(píng)論 2 327
  • 正文 我和宋清朗相戀三年裂允,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哥艇。...
    茶點(diǎn)故事閱讀 38,650評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绝编,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出貌踏,到底是詐尸還是另有隱情十饥,我是刑警寧澤,帶...
    沈念sama閱讀 34,329評(píng)論 4 330
  • 正文 年R本政府宣布祖乳,位于F島的核電站逗堵,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏眷昆。R本人自食惡果不足惜蜒秤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望亚斋。 院中可真熱鬧垦藏,春花似錦、人聲如沸伞访。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,757評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)厚掷。三九已至弟灼,卻和暖如春级解,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背田绑。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,991評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工勤哗, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人掩驱。 一個(gè)月前我還...
    沈念sama閱讀 46,370評(píng)論 2 360
  • 正文 我出身青樓芒划,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親欧穴。 傳聞我的和親對(duì)象是個(gè)殘疾皇子民逼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容