Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QMUIButton在UITableViewCell中遇到的布局问题 #393

Closed
4 tasks done
fanyuecheng opened this issue Aug 31, 2018 · 4 comments
Closed
4 tasks done

QMUIButton在UITableViewCell中遇到的布局问题 #393

fanyuecheng opened this issue Aug 31, 2018 · 4 comments

Comments

@fanyuecheng
Copy link

fanyuecheng commented Aug 31, 2018

请填写运行环境

  • 设备:模拟器
  • 系统:iOS 11.4
  • Xcode 版本:9.4.1
  • QMUI iOS 版本:2.7.6

请描述具体问题

使用Masonry布局
UITableViewCell中有一View,有一QMUIButton添加到上面,其left,top,height固定,长度根据其titleLabel.text 自适应

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.bgView).offset(10);
        make.height.mas_equalTo(60);
    }];
}

cell在重用过程中,QMUIButton的width正确,但是titleLabel的width会有错误。系统UIButton并无此问题。

解决办法有两种:
1.该button添加到cell.contentView上
2.修改QMUIButton的layoutSubviews方法

BOOL isImageViewShowing =  self.imageView && !self.imageView.hidden;   // !!self.currentImage;  

前者和后者 BOOL 值相同,推测前者触发了什么布局方法。

相关截图(断点的堆栈、控制台的 log)

黄色为QMUIButton 的superView,红色为QMUIButton,绿色为button.titleLabel
2018-08-31 2 44 41

@MoLice
Copy link
Collaborator

MoLice commented Aug 31, 2018

请把你这个界面的代码文件(viewController + cell)发送到 qmuiteam@qq.com

@fanyuecheng
Copy link
Author

fanyuecheng commented Sep 3, 2018

文件已经发送了,可你们回复"看你的代码,好像并没有看到你 issue 里说的“让 button 的宽度跟随title的变化而变化”?",只是看并没有运行或者怎样码?
我不知道是你们没理解我的意思,还是我表达不够确切
既然如此,我再仔仔细细的说一下
即一cell,其上先添加一subview,再往该subview上添加一QMUIButton,该button仅设置top,left,height三个约束,或right.lessThanOrEqualTo(button.superview).offset(-10),不管怎样,让button随其titleLabel的长度自由伸缩
这样TableView在滑动过程中,不断的给button设置长度不一的title,button会长度不一,但是其titleLabel的frame却出现错误,就是这样.很难理解??

@MoLice
Copy link
Collaborator

MoLice commented Sep 11, 2018

经测试,这与以下几个因素有关:

  1. UIButtonimageView 是懒加载的,在 getter 被访问的时候,某些时候会触发 setupImageView,紧接着触发 button 额外的 layout,这也是为什么你改为用 self.imageView && !self.imageView.hidden 能规避这个问题。
  2. UITableViewCell.contentView 会有自己一套“主动触发 subview 的 layoutSubviews” 的机制,这也是为什么当你把 button 直接放到 contentView 上就没问题,原因类似第 1 点。

而只要把 QMUIButton 里访问到 imageViewtitleLabel 的地方都加上 isImageShowingisTitleLabelShowing 的判断即可避免这一系列问题。请等待后续的版本发布,在此之前可以改为用 frame 布局写法,或者将 button 作为 cell.contentView 的 subview。

@MoLice
Copy link
Collaborator

MoLice commented Sep 21, 2018

2.8.0 已修复

@MoLice MoLice closed this as completed Sep 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants