iOS之UIButton的封装和常用属性\按钮UIButton的常用属性及方法总结(二)、多按钮排列、cell上多图片布局

本文主要是介绍iOS之UIButton的封装和常用属性\按钮UIButton的常用属性及方法总结(二)、多按钮排列、cell上多图片布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

-(void)addbtn{

  

//UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];

    // 设置自定义的按钮

UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];//UIButtonTypeRoundedRect是设置一个圆角按钮

// 按钮的位置坐标

    btn.frame=CGRectMake(80,250,250,30);

    

// 普通状态按钮标题

    [btn setTitle:@"Button"forState:UIControlStateNormal];

//  高亮状态的按钮标题,点击按钮时会高亮

    [btn setTitle:@"高亮状态" forState:UIControlStateHighlighted];//

    

// 高亮状态光晕效果

    [btn setShowsTouchWhenHighlighted:YES];

    

//设置标题的颜色

    [btn setTitleColor:[UIColorredColor]forState:UIControlStateNormal];

//设置高亮时的标题的颜色

    [btn setTitleColor:[UIColorgreenColor]forState:UIControlStateHighlighted];

    

// 设置标题的字体大小

    [btn.titleLabelsetFont:[UIFontboldSystemFontOfSize:20]];

    btn.titleLabel.font = [UIFontsystemFontOfSize:16];

    btn.titleLabel.font=[UIFontsystemFontOfSize:17weight:UIFontWeightBold];

    

//设置背景颜色

    [btn setBackgroundColor:[UIColorblueColor]];

    

// 图片被拉伸式地设置背景图片

    [btn setBackgroundImage:[UIImageimageNamed:@"bankYellow"]forState:UIControlStateNormal];

//  按钮被选中时的背景图片

    [btn setBackgroundImage:[UIImageimageNamed:@"bankBlue"]forState:UIControlStateHighlighted];

    

//设置按钮的图片,超过按钮尺寸会吧标题也覆盖掉

    [btn setImage:[UIImageimageNamed:@"bankYellow"]forState:UIControlStateNormal];

//设置按钮高亮时的图片,也就是按钮点击下去未松开的时候;

    [btn setImage:[UIImageimageNamed:@"bankBlue"]forState:UIControlStateHighlighted];

//设置按钮被选中时的图片,select属性必须设置为yes,选中时的图片才有作用

    btn.selected=YES;

     [btn setImage:[UIImageimageNamed:@"bankBlue"]forState:UIControlStateSelected];

    

//设置按钮内容的对其方式

btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;

 

    //添加按钮的点击事件

    [btn addTarget:self action:@selector(sendNoti:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:btn];

  

}

-(void)sendNoti:(UIButton *)sender{

    threeVC *three=[[threeVCalloc]init];

    [self.navigationControllerpushViewController:threeanimated:YES];

}

 

 

 

=====简单的封装:

 

调用:

  NSDictionary *dict=@{

         @"title":@"第一",//标题

         @"highlightTitle":@"第二",//高亮标题

         @"selectedTitle":@"第三",//选中时的标题

         

         @"titlecolor":[UIColorredColor],//标题颜色

         @"highlighttitlecolor":[UIColorgreenColor],//高亮标题颜色

         @"selectedtitlecolor":[UIColoryellowColor],//选中时标题颜色

 

//         @"image":@"bankYellow",//图片

//         @"highlightimage":@"bankBlue",//高亮图片

//         @"selectedimage":@"bankGreen",//选中时的图片

//

//         @"bgimage":@"bankYellow",//背景图片

//         @"highlightbgimage":@"bankBlue",//高亮背景图片

//         @"selectedbgimage":@"bankGreen",//选中时的背景图片

//

 

         @"bgcolor":[UIColorblueColor],//背景颜色

         @"highlightbgcolor":[UIColorgreenColor],//高亮时背景颜色

         @"selectedbgcolor":[UIColorredColor],//选中时的背景颜色

         

         @"isSetShowsTouchWhenHighlighted":[NSNumbernumberWithBool:YES],// 高亮状态是否有光晕效果,BOOl类型

         @"fontsize":[NSNumbernumberWithInteger:30],//标题大小NSInteger类型

         @"btnaligmentmark":@"",//按钮内容对齐方式的标识,1左对齐,2右对齐,3填充,其他居中NSInteger类型 ,[NSNumber numberWithInteger:1];

         @"isSelectedBtn":[NSNumbernumberWithBool:YES]

                         };

   

    LYBaseBtn *btn=[[LYBaseBtnalloc]initWithFrame:CGRectMake(0,100,100, 50withDict:dict];

    //添加按钮的点击事件

    [btn addTarget:self action:@selector(sendNoti:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btn];

 

btn封装:

 

#import <UIKit/UIKit.h>

 

@interface LYBaseBtn : UIButton

/**

 *arr    参数数组

 */

-(instancetype)initWithFrame:(CGRect)frame withDict:(NSDictionary *)dict;

@property(nonatomic,copy)NSString *title;//标题

@property(nonatomic,copy)NSString *highlightTitle;//高亮标题

@property(nonatomic,copy)NSString *selectedTitle;//选中时的标题

 

@property(nonatomic,copy)NSString *image;//图片

@property(nonatomic,copy)NSString *highlightimage;//高亮图片

@property(nonatomic,copy)NSString *selectedimage;//选中时的图片

 

@property(nonatomic,copy)NSString *bgimage;//背景图片

@property(nonatomic,copy)NSString *highlightbgimage;//高亮背景图片

@property(nonatomic,copy)NSString *selectedbgimage;//选中时的背景图片

 

@property(nonatomic,strong)UIColor *titlecolor;//标题颜色

@property(nonatomic,strong)UIColor *highlighttitlecolor;//高亮标题颜色

@property(nonatomic,strong)UIColor *selectedtitlecolor;//选中标题颜色

 

@property(nonatomic,strong)UIColor *bgcolor;//背景颜色

@property(nonatomic,strong)UIColor *highlightbgcolor;//高亮背景颜色

@property(nonatomic,strong)UIColor *selectedbgcolor;//选中背景颜色

 

@property(nonatomic,assign)BOOL isSetShowsTouchWhenHighlighted;// 高亮状态是否有光晕效果

 

@property(nonatomic,assign)UIControlContentHorizontalAlignment btnalignment;//按钮内容个对齐方式

 

@property(nonatomic,assign)NSInteger btnaligmentmark;//对齐方式的标志

 

 

@property(nonatomic,assign)NSInteger fontsize;//字体大小

 

@property(nonatomic,assign)BOOL isSelectedBtn;//按钮是否选中

 

@end

-------

 

#import "LYBaseBtn.h"

 

@implementation LYBaseBtn

-(instancetype)initWithFrame:(CGRect)frame withDict:(NSDictionary *)dict{

    if(self==[superinitWithFrame:frame]){

       

        [selfsetAttributeWithDict:dict];//设置属性

        [self addbtn];//添加按钮

    }

    return self;

}

 

-(void)addbtn{

    if(self.title){

    // 普通状态按钮标题

    [selfsetTitle:self.titleforState:UIControlStateNormal];

    }

    if(self.highlightTitle){

    //  高亮状态的按钮标题,点击按钮时会高亮

        [selfsetTitle:self.highlightTitleforState:UIControlStateHighlighted];

    }

    if(self.selectedTitle &&self.isSelectedBtn){

        //  高亮状态的按钮标题,点击按钮时会高亮

        [selfsetTitle:self.selectedTitleforState:UIControlStateSelected];

    }

    

    if(self.isSetShowsTouchWhenHighlighted){

    // 高亮状态光晕效果

    [selfsetShowsTouchWhenHighlighted:self.isSetShowsTouchWhenHighlighted];

    }

    

    if(self.titlecolor){

    //设置标题的颜色

    [selfsetTitleColor:self.titlecolorforState:UIControlStateNormal];

    }

    if(self.highlighttitlecolor){

    //设置高亮时的标题的颜色

    [selfsetTitleColor:self.highlighttitlecolorforState:UIControlStateHighlighted];

    }

    if(self.selectedtitlecolor&&self.isSelectedBtn){

        //设置选中时的标题的颜色

        [selfsetTitleColor:self.selectedtitlecolorforState:UIControlStateSelected];

    }

    

    if(self.fontsize){

// 设置标题的字体大小

    self.titleLabel.font=[UIFontsystemFontOfSize:self.fontsizeweight:UIFontWeightBold];

    }

    

    if(self.bgcolor){

    //设置背景颜色

    [selfsetBackgroundColor:self.bgcolor];

    }

    if(self.highlightbgcolor){

        //设置高亮背景颜色

        [selfsetBackgroundColor:self.highlightbgcolor];

    }

    if(self.selectedbgcolor&&self.isSelectedBtn){

        //设置选中时的背景颜色

        [selfsetBackgroundColor:self.selectedbgcolor];

    }

    

    

    if(self.bgimage){

    // 图片被拉伸式地设置背景图片

    [selfsetBackgroundImage:[UIImageimageNamed:self.bgimage]forState:UIControlStateNormal];

    }

     if(self.highlightbgimage){

    //  按钮高亮时的背景图片

    [selfsetBackgroundImage:[UIImageimageNamed:self.highlightbgimage]forState:UIControlStateHighlighted];

     }

    if(self.selectedimage&&self.isSelectedBtn){

        //  按钮选中时的背景图片

        [selfsetBackgroundImage:[UIImageimageNamed:self.selectedimage]forState:UIControlStateSelected];

    }

    

    

    if(self.image){

    //设置按钮的图片,超过按钮尺寸会吧标题也覆盖掉

    [selfsetImage:[UIImageimageNamed:self.image]forState:UIControlStateNormal];

    }

   if(self.highlightimage){

    //设置按钮高亮时的图片,也就是按钮点击下去未松开的时候;

       [selfsetImage:[UIImageimageNamed:self.highlightimage]forState:UIControlStateHighlighted];

   }

    if(self.selectedimage&&self.isSelectedBtn){

    //设置按钮被选中时的图片,select属性必须设置为yes,选中时的图片才有作用

    self.selected=YES;

    [selfsetImage:[UIImageimageNamed:self.selectedimage]forState:UIControlStateSelected];

    }

    

    if(self.btnalignment){

    //设置按钮内容的对其方式

     self.contentHorizontalAlignment=self.btnalignment;

    }

}

 

-(void)setAttributeWithDict:(NSDictionary *)dict{

//标题

    if([[dictallKeys]containsObject:@"title"]&& ![dict[@"title"]isEqualToString:@""]){

     self.title=dict[@"title"];

    }

//高亮标题

    if([[dictallKeys]containsObject:@"highlightTitle"]&& ![dict[@"highlightTitle"]isEqualToString:@""]){

    self.highlightTitle=dict[@"highlightTitle"];

    }

//选中标题

    if([[dictallKeys]containsObject:@"selectedTitle"]&& ![dict[@"selectedTitle"]isEqualToString:@""]){

    self.selectedTitle=dict[@"selectedTitle"];

    }

    

//*********标题颜色

    if([[dictallKeys]containsObject:@"titlecolor"] && [dict[@"titlecolor"]isKindOfClass:[UIColorclass]]){

        self.titlecolor=dict[@"titlecolor"];

    }

    //高亮标题颜色

    if([[dictallKeys]containsObject:@"highlighttitlecolor"] && [dict[@"highlighttitlecolor"]isKindOfClass:[UIColorclass]]){

        self.highlighttitlecolor=dict[@"highlighttitlecolor"];

    }

    //选中标题颜色

    if([[dictallKeys]containsObject:@"selectedtitlecolor"] && [dict[@"selectedtitlecolor"]isKindOfClass:[UIColorclass]]){

        self.selectedtitlecolor=dict[@"selectedtitlecolor"];

    }

//****按钮图片

     if([[dictallKeys]containsObject:@"image"]&& ![dict[@"image"]isEqualToString:@""]){

    self.image=dict[@"image"];

     }

//按钮高亮图片

     if([[dictallKeys]containsObject:@"highlightimage"]&& ![dict[@"highlightimage"]isEqualToString:@""]){

    self.highlightimage=dict[@"highlightimage"];

     }

//按钮选中图片

    if([[dictallKeys]containsObject:@"selectedimage"]&& ![dict[@"selectedimage"]isEqualToString:@""]){

    self.selectedimage=dict[@"selectedimage"];

    }

//********背景图片

     if([[dictallKeys]containsObject:@"bgimage"]&& ![dict[@"bgimage"]isEqualToString:@""]){

    self.bgimage=dict[@"bgimage"];

     }

//高亮背景图片

    if([[dictallKeys]containsObject:@"highlightbgimage"]&& ![dict[@"highlightbgimage"]isEqualToString:@""]){

    self.highlightbgimage=dict[@"highlightbgimage"];

    }

//选中的背景图片

     if([[dictallKeys]containsObject:@"selectedbgimage"]&& ![dict[@"selectedbgimage"]isEqualToString:@""]){

    self.selectedbgimage=dict[@"selectedbgimage"];

     }

 

    

//*********背景颜色

     if([[dictallKeys]containsObject:@"bgcolor"] && [dict[@"bgcolor"]isKindOfClass:[UIColorclass]]){

    self.bgcolor=dict[@"bgcolor"];

     }

//高亮背景颜色

    if([[dictallKeys]containsObject:@"highlightbgcolor"] && [dict[@"highlightbgcolor"]isKindOfClass:[UIColorclass]]){

        self.highlightbgcolor=dict[@"highlightbgcolor"];

    }

//选中背景颜色

    if([[dictallKeys]containsObject:@"selectedbgcolor"] && [dict[@"selectedbgcolor"]isKindOfClass:[UIColorclass]]){

        self.selectedbgcolor=dict[@"selectedbgcolor"];

    }

   

//选中时是否有光晕

     if([[dictallKeys]containsObject:@"isSetShowsTouchWhenHighlighted"]&& [dict[@"isSetShowsTouchWhenHighlighted"]boolValue]!=0){

self.isSetShowsTouchWhenHighlighted=[dict[@"isSetShowsTouchWhenHighlighted"]boolValue];

     }else {

         self.isSetShowsTouchWhenHighlighted=NO;

     }

    

//标题大小

 if([[dictallKeys]containsObject:@"fontsize"]&& [dict[@"fontsize"]integerValue]!=0){

     self.fontsize=[dict[@"fontsize"]integerValue];

 }else {

     self.fontsize=17;

 }

//按钮内容对齐方式

    if([[dictallKeys]containsObject:@"btnaligmentmark"] && [dict[@"btnaligmentmark"]integerValue]){

        if([dict[@"btnaligmentmark"]integerValue] ==1){

            self.btnalignment=UIControlContentHorizontalAlignmentLeft;

        }else if([dict[@"btnaligmentmark"]integerValue] ==2){

            self.btnalignment=UIControlContentHorizontalAlignmentRight;

        }else if([dict[@"btnaligmentmark"]integerValue] ==3){

            self.btnalignment=UIControlContentHorizontalAlignmentFill;

        } else{

            self.btnalignment=UIControlContentHorizontalAlignmentCenter;

        }

    }else {

        self.btnalignment=UIControlContentHorizontalAlignmentCenter;

    }

    

//按钮是否选中

    if([[dictallKeys]containsObject:@"isSelectedBtn"] && [dict[@"isSelectedBtn"]boolValue]!=0){

        self.isSelectedBtn=[dict[@"isSelectedBtn"]boolValue];

    }else{

        self.isSelectedBtn=NO;

    }

    

}

@end

 

------------UIButton的setBacgrounImage和setImage的使用区别:-----------

在将 UIButton 当做图标按钮使用时,可以有两种方式给它设置一张图片:setBackgroundImage:forState: 和 setImage:forState:。用这两种方式都可以把 UIButton 作为图片按钮使用,这在图片背景的比例和UIButton 的宽高比例相同时是没什么问题的,图片都不会因为被拉伸或者缩放而出现失真。但是当图片的比例和 Button 的尺寸比例不一样时,这两种方式设置图片的效果就不一样了;

如果不做任何设置,setBancegroundinmge和setImage都会使图片被拉升;

对于setImage:forState:设置的图片可以给 UIButton 的 imageView 设置对应的填充属性来解决缩放问题,注意不是直接设置 UIButton 的 contentModesetImage:forState: 会将 image 设置到 UIButton 中的 ImageView 图层,直接设置 UIButton 的 contentMode 是不起作用的。对于用 setBackgroundImage:forState: 方式设置的图片,无论怎么设置都不会有效果,这种方式会直接将图片拉伸至 Button 的边界,来填充满整个 Button。所以如果 图片和 Button的比例不一致时,只能使用 setImage:forState: 这种方式来保证图片不被压缩。 

但是,当 Button 的大小超过了图片的原始大小,并且比例也不一样时,这时候 Button 四周就可能会出现图片覆盖不上的空白;

设置 button.imageView?.contentMode = .ScaleAspectFill 的确会让图片不再变形,但是也只是按图片的原始尺寸显示了,两边的留白也是很不美观的。当然,这也是有解决办法的,UIControl 里提供了两个属性:contentHorizontalAlignment 和 contentVerticalAlignment 分别用来设置水平和竖直方向上内容的对齐方式,把 button1 的 contentHorizontalAlignment 设置为 Fill 就可以解决上面的问题,如果是竖直方向无法填满的话,设置 contentVerticalAlignment 就行了。

 

btn.imageview.contemmode的填充模式:参考:https://www.2cto.com/kf/201507/412894.html

 UIViewContentModeScaleAspectFil:将图片等比例拉伸,会填充整个UIImageView,但是会有一部分过大而超出区域

  • ScaleToFill为:将图片按照整个区域进行拉伸(会破坏图片的比例) 
  • UIViewContentModeScaleAspectFit:将图片等比例拉伸,不会填充满整个区域 ,这个图片都会在view里面显示,并且比例不变 这就是说 如果图片和view的比例不一样 就会有留白
  • UIViewContentModeScaleAspectFill:将图片等比例拉伸,会填充整个区域,但是会有一部分过大而超出整个区域。个view会被图片填满,图片比例不变,如果超出位置的可以截取掉;

    [self.prp_imageViewsetContentMode:UIViewContentModeScaleAspectFill];

    self.prp_imageView.clipsToBounds = YES;


    至于Top,Left,Right等等就是将图片在view中的位置进行调整。

其其他模式:

/    UIViewContentModeRedraw,

//    UIViewContentModeCenter,           

//    UIViewContentModeTop,

//    UIViewContentModeBottom,

//    UIViewContentModeLeft,

//    UIViewContentModeRight,

//    UIViewContentModeTopLeft,

//    UIViewContentModeTopRight,

//    UIViewContentModeBottomLeft,

//    UIViewContentModeBottomRight,

 

 

----------按钮中内容的对其方式-------

 

btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;

 

 UIControlContentHorizontalAlignmentCenter = 0,

    UIControlContentHorizontalAlignmentLeft   = 1,

    UIControlContentHorizontalAlignmentRight  = 2,

    UIControlContentHorizontalAlignmentFill   = 3,

    UIControlContentHorizontalAlignmentLeading  = 4,

    UIControlContentHorizontalAlignmentTrailing

 

 

 

 

btn.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;

 

UIControlContentVerticalAlignmentCenter  = 0,

 

    UIControlContentVerticalAlignmentTop     = 1,

    UIControlContentVerticalAlignmentBottom  = 2,

    UIControlContentVerticalAlignmentFill    = 3,

 

--------titleEdge和imageEdge-------

 

https://blog.csdn.net/u011146511/article/details/73111859

 

----------多按钮排列

@interface LYHomeTabHeaderview()@property(nonatomic,strong)NSArray *imageArr;
@property(nonatomic,strong)NSArray *titleArr;@end
@implementation LYHomeTabHeaderview
-(NSArray *)titleArr{if(nil==_titleArr){_titleArr=@[@"安心门店",@"每日签到",@"店面体验",@"拍卖",@"论坛",@"会员卡充值",@"教育",@"介绍"];}return _titleArr;
}-(NSArray *)imageArr{if(nil==_imageArr){_imageArr=@[@"anxinmendian",@"meiriqiandao",@"dianmiantiyan",@"anxinpaimai",@"anxinluntan",@"huiyuankachongzhi",@"anxinjiaoyu",@"qiyejieshao"];}return _imageArr;
}
-(instancetype)initWithFrame:(CGRect)frame{if(self==[super initWithFrame:frame]){[self initviews];}return self;
}-(void)initviews{[self fourbtnView];//四个按钮}-(void)fourbtnView{UIView *fourbtnView=[[UIView alloc]initWithFrame:CGRectMake(0, 183, WIDTH, 99+79)];fourbtnView.backgroundColor=[UIColor whiteColor];UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];line.backgroundColor=[UIColor colorWithHexString:@"#f6f6f6" alpha:1];[fourbtnView addSubview:line];[self addSubview:fourbtnView];CGFloat w=44;//图片宽度和高度CGFloat margin=25;//水平间隔CGFloat midsep=(WIDTH-(44+20)*4-margin*2)/3;CGFloat h=66;//竖向跨度CGFloat y=15;//距顶部的距离NSInteger count=4;//一排4个for (int i=0; i<8; i++) {UIView *bv=[[UIView alloc]initWithFrame:CGRectMake(margin+i%count*(w+20+midsep),i/count*(y+h), w+20,y+h)];[fourbtnView addSubview:bv];UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10,y, w, w)];btn.tag=9999+i;[btn setImage:[UIImage imageNamed:self.imageArr[i]] forState:UIControlStateNormal];[btn addTarget:self action:@selector(fourbtnViewclick:) forControlEvents:UIControlEventTouchUpInside];[bv addSubview:btn];UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0,y+w+12, w+20, 12)];lbl.tag=9999+i;lbl.text=self.titleArr[i];lbl.textAlignment=NSTextAlignmentCenter;lbl.textColor=[UIColor colorWithHexString:@"#121212" alpha:1];lbl.font=[UIFont systemFontOfSize:12];[bv addSubview:lbl];}}-(void)fourbtnViewclick:(UIButton *)btn{
//    btn.enabled=NO;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        btn.enabled=YES;
//    });
//    NSLog(@"五个按钮");NSInteger index=btn.tag-9999;NSString *titlestr=@"";if(index==0){//门店titlestr=@"门店";}else if(index==1){//每日签到titlestr=@"每日签到";}else if(index==2){//店面体验titlestr=@"店面体验";}else if(index==3){//拍卖titlestr=@"拍卖";}else if(index==4){//论坛titlestr=@"论坛";}else if(index==5){//会员卡充值titlestr=@"会员卡充值";}else if(index==6){//教育titlestr=@"教育";}else if(index==7){//介绍titlestr=@"介绍";}}

*********cell上多个图片布局

//设置cell上的内容哦
-(CGFloat)setcellWithDict:(NSDictionary *)dict{NSString *nickname=dict[@"nickname"];//昵称[self.nickBtn setTitle:nickname forState:UIControlStateNormal];self.imagV.image=[UIImage imageNamed:dict[@"img"]];//头像self.contetnlbl.text=dict[@"content"];//文本内容NSArray *imgArr=dict[@"imgcontent"];//图片名数组//*****计算文本的高度 *******给显示的文本一个区域*****CGSize contentMaxSizes = CGSizeMake(WIDTH-40, MAXFLOAT);// NSFontAttributeName 字体的大小NSDictionary *attributesDicts = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};//计算文本实际宽高的时候, 计算的字体大小要和label中设置的字体大小保持一致// 根据限定的条件, 来计算text 真实的宽高CGSize contentRealSizes =  [self.contetnlbl.text boundingRectWithSize:contentMaxSizes options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDicts context:nil].size;self.contetnlbl.height=contentRealSizes.height;// 重新设置frame/*单图会按照图片等比例显示多图的图片大小固定多图如果是4张,会按照 2 * 2 显示多图其他数量,按照 3 * 3 九宫格显示*/CGFloat imgcontentviewheight=0;NSInteger w=(WIDTH-40)/3;NSInteger count=imgArr.count;NSInteger row=(count-1)/3+1;//确定行数CGFloat h=0;//cell 的高度self.imgContentView.y=CGRectGetMaxY(self.contetnlbl.frame);if(imgArr.count==0){self.imgContentView.height=imgcontentviewheight;}else {self.imgContentView.height=row*w;}//******清空imgContentView上的内容*******if(self.imgContentView){for (UIView *v in self.imgContentView.subviews) {[v removeFromSuperview];}}//***********创建图片for(int i=0;i<count;i++){UIImageView *conteviewimage=[[UIImageView alloc]initWithFrame:CGRectMake(i%3*w,i/3*w, w, w)];conteviewimage.image=[UIImage imageNamed:imgArr[i]];[self.imgContentView addSubview:conteviewimage];}self.bottomView.y=CGRectGetMaxY(self.imgContentView.frame);if(count==0){h=CGRectGetMaxY(self.imagV.frame)+contentRealSizes.height+50+50;}else{h=CGRectGetMaxY(self.imagV.frame)+contentRealSizes.height+50+row*w+20+50;}return  h;
}

 

这篇关于iOS之UIButton的封装和常用属性\按钮UIButton的常用属性及方法总结(二)、多按钮排列、cell上多图片布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1017741

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta