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

相关文章

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W