iOS13.6.1系统XR手机出来了奇怪的问题,图文按钮显示不全问题。比它老的系统或新的系统就是相同的手机都没有出现类似的问题。就是系统相同手机不同也没有出现该问题。
既然测试提出该问题,只能修改了。这是特定手机特定系统适配问题。
修改的思路是:把这个显示图片和文字的按钮拆分成按钮包含一个图片和一个标签,计算出他们的文字宽和图片宽。若有这样大量图文按钮,若找不到对应页面,都这样修改,有可能出现布局不正确的问题。我就是这样的,产生了好几个相关的问题,如:计算文字宽度的小数被舍去了和布局的位置不正确。真不知道为了一个手机在特定的系统下出现该问题,进行大批量修改,并产生了一堆问题是否值得。
出问题的代码:
UIButton *btn = [UIButton buttonWithTitle:@" 添加车辆" font:SystemFontOfSize(15) color:MainAPPColor]; [btn setImage:[UIImage imageNamed:@"icon_car_add"] forState:UIControlStateNormal]; [btn layoutButtonWithEdgeInsetsStyle: imageTitleSpace:2];
注意:用设置UISemanticContentAttributeForceLeftToRight或标题加空格,采用分类:UIButton+Commemt.h都搞不定这个手机。另一个带选中效果的问题代码:
UIButton *btn = [UIButton buttonWithTitle:@" 0" font:SystemFontOfSize(13) color:TextBlackColor]; [btn setTitleColor:MainAPPColor forState:UIControlStateSelected]; [btn setImage:[UIImage imageNamed:@"icon_like_no"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"icon_like_yes"] forState:UIControlStateSelected]; [btn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside]; [btn setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight]; [btn layoutButtonWithEdgeInsetsStyle:JKButtonEdgeInsetsStyleLeft imageTitleSpace:2]; [self.contentView addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(BaseSize(110)); make.width.mas_equalTo(size.width); make.bottom.mas_equalTo(-BaseSize(10)); make.height.mas_equalTo(BaseSize(24)); }]; self.likeBtn = btn;
万能解决方法是生成一个按钮子类,上面增加一个图片和标签:
DYTitleImageBtn.h
#import <UIKit/UIKit.h> //#import "UIButton+Commemt.h" // 定义一个枚举(包含了四种类型的button) typedef NS_ENUM(NSUInteger, JKButtonEdgeInsetsStyle) { JKButtonEdgeInsetsStyleTop, // image在上,label在下 JKButtonEdgeInsetsStyleLeft, // image在左,label在右 JKButtonEdgeInsetsStyleBottom, // image在下,label在上 JKButtonEdgeInsetsStyleRight // image在右,label在左 }; NS_ASSUME_NONNULL_BEGIN @interface DYTitleImageBtn : UIButton @property (nonatomic,strong)UIImageView *jk_iconImageView; @property (nonatomic, assign) CGFloat imageTitleSpace; @property (nonatomic, strong) UILabel *jk_titleLab; @property (nonatomic, assign) BOOL isSelectContent; @property (nonatomic, strong) UIImage *icon; @property (nonatomic, strong) UIImage *selectedIcon; @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *selectedTitle; @property (nonatomic, assign) JKButtonEdgeInsetsStyle style; @property (nonatomic, strong) UIColor *titleColor; @property (nonatomic, strong) UIColor *selectedTitleColor; + (instancetype)btnWithFrame:(CGRect)frame title:(NSString *)title selectedTitle:(NSString *)selectedTitle image:(UIImage *)image selectedImage:(UIImage *)selectedImage font:(UIFont *)font color:(UIColor *)color selectedTitleColor:(UIColor *)selectedTitleColor style:(JKButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)imageTitleSpace; + (CGSize)getSizeWithMaxTitle:(NSString *)maxTitle maxImage:(UIImage *)maxImage font:(UIFont *)font imageTitleSpace:(CGFloat)imageTitleSpace heigth:(CGFloat)height; -(void)updateLayoutWithFrame:(CGRect)frame title:(NSString *)title; -(void)updateLayoutWithFrame:(CGRect)frame title:(NSString *)title icon:(UIImage *)icon; -(void)updateBtnLayoutWithFrame:(CGRect)frame title:(NSString *)title; @end NS_ASSUME_NONNULL_END
DYTitleImageBtn.m文件:
#import "DYTitleImageBtn.h" @implementation DYTitleImageBtn + (instancetype)btnWithFrame:(CGRect)frame title:(NSString *)title selectedTitle:(NSString *)selectedTitle image:(UIImage *)image selectedImage:(UIImage *)selectedImage font:(UIFont *)font color:(UIColor *)color selectedTitleColor:(UIColor *)selectedTitleColor style:(JKButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)imageTitleSpace; { DYTitleImageBtn *btn = [[self alloc] initWithFrame:frame]; btn.imageTitleSpace = imageTitleSpace; if(!image || ![image isKindOfClass:[UIImage class]] || !selectedImage || ![selectedImage isKindOfClass:[UIImage class]] || !font || ![font isKindOfClass:[UIFont class]]) { [btn setTitle:getNotNilString(title) forState:UIControlStateNormal]; if(font && [font isKindOfClass:[UIFont class]]) { btn.titleLabel.font = font; } if(color && [color isKindOfClass:[UIColor class]]) { [btn setTitleColor:color forState:UIControlStateNormal]; } return btn; } else { btn.backgroundColor = UIColor.clearColor; btn.icon = image; btn.selectedIcon = selectedImage; btn.selectedTitleColor = selectedTitleColor; btn.title = getNotNilString(title); btn.selectedTitle = getNotNilString(selectedTitle); btn.jk_iconImageView = [[UIImageView alloc] init]; [btn addSubview:btn.jk_iconImageView]; btn.jk_iconImageView.backgroundColor = UIColor.clearColor; btn.jk_iconImageView.image = image; btn.jk_titleLab = [[UILabel alloc] init]; [btn addSubview:btn.jk_titleLab]; btn.jk_titleLab.backgroundColor = UIColor.clearColor; btn.jk_titleLab.font = font; btn.jk_titleLab.text = getNotNilString(btn.title); if(color && [color isKindOfClass:[UIColor class]]) { btn.jk_titleLab.textColor = color; } else { btn.jk_titleLab.textColor = TextBlackColor; } if(JKButtonEdgeInsetsStyleRight == style) { btn.style = style; btn.jk_titleLab.textAlignment = NSTextAlignmentRight; btn.jk_iconImageView.sd_layout .rightSpaceToView(btn, 0) .centerYEqualToView(btn) .widthIs(image.size.width) .heightIs(image.size.height); btn.jk_titleLab.sd_layout .leftSpaceToView(btn, 0) .centerYEqualToView(btn) .rightSpaceToView(btn.jk_iconImageView, imageTitleSpace) .heightIs(frame.size.height); } else { btn.style = JKButtonEdgeInsetsStyleLeft; btn.jk_titleLab.textAlignment = NSTextAlignmentLeft; btn.jk_iconImageView.sd_layout .leftSpaceToView(btn, 0) .centerYEqualToView(btn) .widthIs(image.size.width) .heightIs(image.size.height); btn.jk_titleLab.sd_layout .rightSpaceToView(btn, 0) .centerYEqualToView(btn) .leftSpaceToView(btn.jk_iconImageView, imageTitleSpace) .heightIs(frame.size.height); } return btn; } } -(void)setIsSelectContent:(BOOL)isSelectContent { _isSelectContent = isSelectContent; if(isSelectContent) { self.jk_iconImageView.image = self.selectedIcon; self.jk_titleLab.text = self.selectedTitle; if(self.selectedTitleColor && [self.selectedTitleColor isKindOfClass:[UIColor class]]) { self.jk_titleLab.textColor = self.selectedTitleColor; } } else { self.jk_iconImageView.image = self.icon; self.jk_titleLab.text = self.title; if(self.titleColor && [self.titleColor isKindOfClass:[UIColor class]]) { self.jk_titleLab.textColor = self.titleColor; } } } + (CGSize)getSizeWithMaxTitle:(NSString *)maxTitle maxImage:(UIImage *)maxImage font:(UIFont *)font imageTitleSpace:(CGFloat)imageTitleSpace heigth:(CGFloat)height { if(isCommonUnitEmptyString(maxTitle) || !maxImage || ![maxImage isKindOfClass:[UIImage class]] || !font || ![font isKindOfClass:[UIFont class]]) { return CGSizeMake(60, 18); } NSDictionary *attributes = @{NSFontAttributeName :font}; //字体属性,设置字体的font CGSize maxSize = CGSizeMake(FULL_WIDTH, FULL_HEIGHT); //设置字符串的宽高 CGSize size = [maxTitle boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; CGFloat labelWidth = size.width; if((labelWidth != ((NSInteger)labelWidth))) { labelWidth = labelWidth + 0.5; } CGFloat height1 = height; if(height1 < size.height) { height1 = size.height; } if(height1 < maxImage.size.height) { height1 = maxImage.size.height; } return CGSizeMake(labelWidth+imageTitleSpace+maxImage.size.width, height1); } -(void)updateLayoutWithFrame:(CGRect)frame title:(NSString *)title { CGFloat titleLabWidth = frame.size.width - self.imageTitleSpace - self.icon.size.width; if(self.jk_titleLab && titleLabWidth > 0) { self.title = getNotNilString(title); self.selectedTitle = getNotNilString(title); self.jk_titleLab.text = self.title; self.jk_titleLab.sd_layout .widthIs(titleLabWidth) .centerYEqualToView(self) .leftSpaceToView(self.jk_iconImageView, self.imageTitleSpace) .heightIs(self.frame.size.height); [self.jk_titleLab updateLayout]; } } -(void)updateBtnLayoutWithFrame:(CGRect)frame title:(NSString *)title { self.frame = frame; CGFloat titleLabWidth = frame.size.width - self.imageTitleSpace - self.icon.size.width; if(self.jk_titleLab && titleLabWidth > 0) { self.title = getNotNilString(title); self.selectedTitle = getNotNilString(title); self.jk_titleLab.text = self.title; self.jk_titleLab.frame = CGRectMake(self.imageTitleSpace + self.icon.size.width, 0, titleLabWidth, frame.size.height); } } -(void)updateLayoutWithFrame:(CGRect)frame title:(NSString *)title icon:(UIImage *)icon { self.frame = frame; if(isCommonUnitEmptyString(title) || !icon) { return; } self.icon = icon; self.selectedIcon = icon; CGFloat titleLabWidth = frame.size.width - self.imageTitleSpace - self.icon.size.width; if(self.jk_iconImageView) { if(JKButtonEdgeInsetsStyleRight == self.style) { self.jk_iconImageView.sd_layout .rightSpaceToView(self, 0) .centerYEqualToView(self) .widthIs(self.icon.size.width) .heightIs(self.icon.size.height); } else { self.jk_iconImageView.sd_layout .leftSpaceToView(self, 0) .centerYEqualToView(self) .widthIs(self.icon.size.width) .heightIs(self.icon.size.height); } [self.jk_iconImageView updateLayout]; } if(self.jk_titleLab && titleLabWidth > 0) { self.title = getNotNilString(title); self.selectedTitle = getNotNilString(title); self.jk_titleLab.text = self.title; self.jk_titleLab.sd_layout .widthIs(titleLabWidth) .centerYEqualToView(self) .leftSpaceToView(self.jk_iconImageView, self.imageTitleSpace) .heightIs(frame.size.height); [self.jk_titleLab updateLayout]; } } @end
使用的代码:
CGSize size = [DYTitleImageBtn getSizeWithMaxTitle:@"9999" maxImage:[UIImage imageNamed:@"icon_like_no"] font:SystemFontOfSize(13) imageTitleSpace:5 heigth:BaseSize(24)]; DYTitleImageBtn *btn = [DYTitleImageBtn btnWithFrame:CGRectMake(0, 0, size.width, size.height) title:@"0" selectedTitle:@"0" image:[UIImage imageNamed:@"icon_like_no"] selectedImage:[UIImage imageNamed:@"icon_like_yes"] font:SystemFontOfSize(13) color:TextBlackColor selectedTitleColor:MainAPPColor style:JKButtonEdgeInsetsStyleLeft imageTitleSpace:5.0]; [btn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(BaseSize(110)); make.width.mas_equalTo(size.width); make.bottom.mas_equalTo(-BaseSize(10)); make.height.mas_equalTo(BaseSize(24)); }]; self.likeBtn = btn;
更新数据:
- (void)likeAction:(DYTitleImageBtn *)sender{ DYWeakSelf; [NetWorkManager postWithPath:Url_Course_Favor param:@{@"CourseId":@(self.model.CourseId)} dataClass:[TSCourseModel class] isArray:NO].complete = ^(DYNetModel *net, TSCourseModel * data) { if (net.code == DYNetModelStateSuccess) { weakSelf.model.FavorCount = data.FavorCount; [sender setSelected:YES]; weakSelf.likeBtn.userInteractionEnabled = NO; // [sender setTitle:[NSString stringWithFormat:@" %ld",(long)data.FavorCount] forState:UIControlStateNormal]; // weakSelf.likeBtn.jk_titleLab.text = [NSString stringWithFormat:@"%ld",(long)data.FavorCount]; CGSize size = [DYTitleImageBtn getSizeWithMaxTitle:[NSString stringWithFormat:@" %ld",(long)data.FavorCount] maxImage:[UIImage imageNamed:@"icon_like_no"] font:SystemFontOfSize(13) imageTitleSpace:5 heigth:BaseSize(24)]; [weakSelf.likeBtn updateLayoutWithFrame:CGRectMake(0, 0, size.width, size.height) title:[NSString stringWithFormat:@" %ld",(long)data.FavorCount]]; [weakSelf.likeBtn mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(BaseSize(110)); make.width.mas_equalTo(size.width); make.bottom.mas_equalTo(-BaseSize(10)); make.height.mas_equalTo(BaseSize(24)); }]; weakSelf.likeBtn.isSelectContent = YES; } }; }
一个测试用他的手机iPhone xr ios 13.6.1测试出来该问题:结果找到iOS13.6.1 iPhone 6手机和iOS 15 iPhone xr手机都测试正常。老板非要修改没有办法,只能把图文按钮拆分为按钮,图文和标签三个控件才能解决。苹果的手机屏幕不是完全相等的,可能他那个手机比标准尺寸小一点点,我见一个同事的手机屏幕高比标准标屏幕多一点点(他的屏幕高度是比标准屏幕高高0.000002)。所以有的问题是特别手机问题,可以不修改,没有必要兼容百分之百的手机,若领导一定要修改,只能用笨方法修改了。