美文网首页
Masonry使用的一些场景的总结-2

Masonry使用的一些场景的总结-2

作者: 西门淋雨 | 来源:发表于2018-07-26 15:28 被阅读2次

Masonry最近才开始使用,还是有点不是很熟悉,下面是一些使用的场景总结。

想实现如下图所示的效果:

image

黑色背景为父试图,会随着标题和内容变化而自动适应高度。

代码如下:

+ (void)makeToast:(OSSessionModel*)model dismisBlock:(AlertDismisBlock)block{

    //OSSessionToastView 即为要显示的整个的弹框

    OSSessionToastView*toast = [[OSSessionToastView alloc]init];

    toast.backgroundColor= [UIColor darkGrayColor];

    toast.layer.cornerRadius=3.0;

    toast.layer.masksToBounds=YES;

    toast.backgroundColor= [UIColor blackColor];

    toast.alpha=1.0;

    toast.model= model;

    toast.alertDismisBlock= block;

    //获取当前的window

    UIWindow*myWindow = [OSSessionToastView getApplicationWindow];

    [myWindow addSubview:toast];

    [toast mas_makeConstraints:^(OSMASConstraintMaker*make) {

        //只设置两个约束,屏幕居中显示

        make.centerX.equalTo(myWindow);

        make.centerY.equalTo(myWindow);

}];

    //添加标题和内容

    [toast showToast:model];

}

- (void)showToast:(OSSessionModel*)model{

    self.model= model;

    //添加label

    [self showLabel];

    //动画

    [UIView animateWithDuration:1.0

    delay:0.0

    options:UIViewAnimationOptionCurveEaseOut

    animations:^{

        self.alpha=1.0;

    }completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5

        delay:2.0

        options:UIViewAnimationOptionCurveEaseIn

    animations:^{

        self.alpha=0.0;    

    }completion:^(BOOLfinished) {

            [self removeFromSuperview];

        }];

    }];

}

- (void)showLabel{

    UIWindow*myWindow = [OSSessionToastView getApplicationWindow];

    //标题

    UILabel*titlelabel = [[UILabel alloc] init];

    titlelabel.backgroundColor= [UIColor clearColor];

    titlelabel.textAlignment=NSTextAlignmentCenter;

    titlelabel.font=DevSystemFontOfSize(18);

    titlelabel.textColor= [UIColor whiteColor];

    titlelabel.text=self.model.title;

    titlelabel.numberOfLines=1;

    [selfaddSubview:titlelabel];

    //内容

    UILabel*contentLa = [[UILabel alloc]init];

    contentLa.backgroundColor= [UIColor clearColor];

    contentLa.textAlignment=NSTextAlignmentCenter;

    contentLa.font=DevSystemFontOfSize(15);

    contentLa.textColor= [UIColor whiteColor];

    contentLa.text=self.model.content;

    contentLa.preferredMaxLayoutWidth= myWindow.bounds.size.width;//最大的宽度是屏幕的宽度

    contentLa.numberOfLines=0;

    [self addSubview:contentLa];

    //标题的约束

    [titlelabel mas_makeConstraints:^(OSMASConstraintMaker*make) {

    make.centerX.equalTo(self);//相对于父试图 居中显示

}];

//内容的约束

[contentLamas_makeConstraints:^(OSMASConstraintMaker*make) {

    make.top.equalTo(titlelabel.mas_lastBaseline).offset(15);//标题的下方15

    make.centerX.equalTo(self);//在父试图中居中显示

    make.left.equalTo(self);//左边和父试图对齐

    make.right.equalTo(self);//右边和父试图对齐

}];

//父试图的约束

[self mas_makeConstraints:^(OSMASConstraintMaker*make) {

    make.top.equalTo(titlelabel).offset(-10);//距离标题10

    make.left.equalTo(contentLa);//和内容左对齐

    make.right.equalTo(contentLa);//和内容右对齐

    make.bottom.equalTo(contentLa).offset(10);//底部和内容距离10

}];

}

+ (UIWindow*)getApplicationWindow

{

    UIWindow*window = [[UIApplicationsharedApplication]keyWindow];

    if(nil==window || window.windowLevel!=UIWindowLevelNormal)

{

        NSArray*windows = [[UIApplicationsharedApplication]windows];

        for(UIWindow* tmpWininwindows)

{

if(tmpWin.windowLevel==UIWindowLevelNormal){
    window = tmpWin;
    break;
      }
    }
  }
  returnwindow;
}

相关文章

  • Masonry使用的一些场景的总结-2

    Masonry最近才开始使用,还是有点不是很熟悉,下面是一些使用的场景总结。 想实现如下图所示的效果: 黑色背景为...

  • Masonry使用的一些场景的总结-1

    Masonry最近才开始使用,还是有点不是很熟悉,下面是一些使用的场景总结。 想实现如下图所示的效果:label1...

  • Autolayout、VFL、Masonry

    适配 VFL语言 Masonry 代码实现Autolayout VFL代码 Masonry使用 总结 使用代码实现...

  • 自动布局之Masonry

    1.什么是Masonry 2.Masonry中的坑 3.Masonry基础API: 4.一些基本使用 5.Wher...

  • 实用iOS第三方框架

    界面布局 github地址:Masonry Masonry使用总结 : 赵不懂的博客 网络请求 github地址...

  • 10.4 Masonry使用-动画

    Masonry使用-动画 会进行上下缩放 Masonry使用-动画1.png Masonry使用-动画2.png

  • Masonry使用总结

    Masonry使用总结 一、Masonry简介 Masonry是一个轻量级的布局框架,适用于iOS以及OS X。它...

  • Masonry自适应UISrollview

    Masonry自适应UISrollview 总结:1.显示视图使用Masonry对srollview作约束,使to...

  • Masonry 自动布局 及注意点

    Masonry和FDTemplateLayoutCell搭配使用总结 http://www.jianshu.com...

  • Masonry

    Masonry使用方法Masonry的使用Masonry的github地址 本篇文章 Masonry的基本使用方法...

网友评论

      本文标题:Masonry使用的一些场景的总结-2

      本文链接:https://www.haomeiwen.com/subject/bsybmftx.html