Masonry——父视图内容,自适应子视图。(view高度自适应)
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIView *container = [UIView new];
[self.view addSubview:container];
container.backgroundColor = [UIColor blueColor];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
//高度自适应,由子view的高度决定。此处切勿设置高度约束。
make.top.equalTo(self.view).offset(100);
make.left.equalTo(self.view).offset(10);
make.right.equalTo(self.view).offset(-10);
}];
UIView *subView = [UIView new];
[container addSubview:subView];
subView.backgroundColor = [UIColor orangeColor];
[subView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(container).offset(10);
make.height.equalTo(@200);
make.left.equalTo(container).offset(10);
make.right.equalTo(container).offset(-10);
//用于撑开container。注意不要设置container高度相关的约束。
make.bottom.equalTo(container).offset(-10);
}];
}
@implementation TitleView
- (void)initSubViews
{
[self addSubview:self.containerView];
[self.containerView addSubview:self.titleLabel];
[self.containerView addSubview:self.countLabel];
}
- (void)addConstraints
{
[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.containerView);
make.bottom.mas_equalTo(self.containerView);
}];
[self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.titleLabel.mas_right).offset(5);
make.bottom.mas_equalTo(self.titleLabel).offset(4);
make.right.mas_equalTo(self.containerView.mas_right);
}];
}
@end
- (void)addConstraints
{
[self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self).offset(40);
make.top.mas_equalTo(self);
make.height.mas_equalTo(30);
// make.width.mas_equalTo(titleViewWidth);
}];
}
Masonry——tableHeaderView 自适应高度。
@implementation RefundStateView
- (void)initSubViews
{
self.backgroundColor = [UIColor clearColor];
self.containerView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.containerView];
[self.containerView addSubview:self.refundProgressView];
[self.containerView addSubview:self.stateTitleLabel];
[self.containerView addSubview:self.stateDetailLabel];
[self.containerView addSubview:self.noticeButton];
}
- (void)didMoveToSuperview
{
[super didMoveToSuperview];
if (self.superview) {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(SCREEN_WIDTH);
make.bottom.mas_equalTo(self.containerView);
}];
[self layoutIfNeeded];
}
}
- (void)addConstraints
{
[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self);
make.bottom.mas_equalTo(self.noticeButton).offset(15);
}];
[self.refundProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.mas_equalTo(self.containerView);
make.top.mas_equalTo(self.containerView).offset(15);
make.height.mas_equalTo(48);
}];
[self.stateTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.refundProgressView.mas_bottom).offset(20);
make.leading.trailing.mas_equalTo(self.containerView).inset(15);
make.height.mas_equalTo(20);
}];
[self.stateDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.stateTitleLabel.mas_bottom).offset(15);
make.leading.trailing.mas_equalTo(self.containerView).inset(60);
//make.height.mas_equalTo(15);
}];
[self.noticeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.stateDetailLabel.mas_bottom).offset(10);
make.leading.trailing.mas_equalTo(self.containerView).inset(15);
make.height.mas_equalTo(40);
}];
}
- (void)setRefundDetailModel:(WSFRefundDetailModel *)refundDetailModel
{
// 2、Label涉及到定时器显示文字时,必须要先给个默认值。不然整个tableHeaderView高度不准确
// @"4天23时56分58秒后未审核系统将默认同意款"
self.stateDetailLabel.text = @"--";
self.refundProgressView.refundProgress = RefundProgressBegin;
_hoursCountDown = [WSFHoursCountDown scheduledTimerWithTimeInterval:9988 handler:^(NSString * _Nonnull timeString) {
NSString *string = [NSString stringWithFormat:@"%@后未审核系统将默认同意款", timeString];
self.stateDetailLabel.text = string;
NSLog(@"currentThread = %@", [NSThread currentThread])
NSMutableAttributedString *attributed = [self setAttributeWithColor:UIColorHex(#FF6666) string:string rangeOfString:timeString];
self.stateDetailLabel.attributedText = attributed;
}];
// 3、containerView内容视图调整
[self layoutIfNeeded];
[self setNeedsDisplay];
if (refundDetailModel.refundDetailModelState == RefundDetailModelCustomerAgreeAllState) {
// 头部:退款状态(有通知)
//self.noticeButton.hidden = NO;
[self.containerView addSubview:self.noticeButton];
[self.noticeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.stateDetailLabel.mas_bottom).offset(10);
make.leading.trailing.mas_equalTo(self.containerView).inset(15);
make.height.mas_equalTo(40);
}];
[self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self);
make.bottom.mas_equalTo(self.noticeButton).offset(15);
}];
} else {
// 头部:退款状态(无通知)
//self.noticeButton.hidden = YES;
[self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self);
make.bottom.mas_equalTo(self.stateDetailLabel).offset(15);
}];
}
}
@end
@implementation RefundDetailVC
- (void)viewDidLoad {
[super viewDidLoad];
// 1、必须先给refundStateView赋值
self.refundStateView.refundDetailModel = self.refundDetailViewModel.refundDetailModel;
// 4、再设置tableHeaderView
self.tableView.tableHeaderView = self.refundStateView;
}
#pragma mark - Getters and Setters
- (WSFRefundStateView *)refundStateView {
if (!_refundStateView) {
_refundStateView = [[WSFRefundStateView alloc] init];
_refundStateView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
}
return _refundStateView;
}
@end
// MARK: - Swift
class MeTableHeaderView: UIView {
lazy var meView: MeView = {
let meView = MeView()
return meView
}()
lazy var titleLabel: UILabel = {
let titleLabel = UILabel.label(font: kFont13, textColor: kRandomColor())
titleLabel.numberOfLines = 0
return titleLabel
}()
lazy var randomButton: UIButton = {
let randomButton = UIButton.button(font: kFont15, textColor: kRandomColor(), title: "随机按钮")
return randomButton
}()
override init(frame: CGRect) {
super.init(frame: frame)
initSubViews()
addConstraints()
}
override func layoutSubviews() {
super.layoutSubviews()
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
if (superview != nil) {
snp.remakeConstraints { (make) in
//make.width.equalTo(superview!)
make.width.equalTo(kScreenWidth)
make.bottom.equalTo(randomButton)
}
layoutIfNeeded()
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initSubViews() {
self.addSubview(meView)
self.addSubview(titleLabel)
self.addSubview(randomButton)
meView.showBlueLayer()
titleLabel.showOrangeLayer()
randomButton.showBlackLayer()
}
func addConstraints() {
meView.snp.makeConstraints { (make) in
make.top.leading.trailing.equalTo(self)
make.bottom.equalTo(meView.titleLabel)
}
titleLabel.snp.makeConstraints { (make) in
make.top.equalTo(meView.snp.bottom)
make.leading.trailing.equalTo(self)
}
randomButton.snp.makeConstraints { (make) in
make.top.equalTo(titleLabel.snp.bottom)
make.leading.trailing.equalTo(self)
make.height.equalTo(45)
}
// self.snp.makeConstraints { (make) in
// make.bottom.equalTo(randomButton)
// }
}
}
class MeVC: BaseVC {
lazy var headerView: MeTableHeaderView = {
let headerView = MeTableHeaderView()
headerView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: 200)
let string = "arc4random_uniform需要一个UInt32类型的参数n,返回一个0 到 n - 1之间的一个类型为UInt32的整数。这种写法相对于第一种来说更安全,具体原因,请参考这里"
headerView.meView.titleLabel.text = string
headerView.titleLabel.text = string
return headerView
}()
override func viewDidLoad() {
super.viewDidLoad()
meTableView.delegate = self
meTableView.dataSource = self
meTableView.rowHeight = 200
meTableView.estimatedRowHeight = 200
meTableView.tableHeaderView = headerView
// meTableView.tableHeaderView?.layoutIfNeeded()
// headerView.layoutIfNeeded()
// headerView.snp.makeConstraints { (make) in
// make.width.equalTo(kScreenWidth)
// make.bottom.equalTo(headerView.randomButton)
// }
}
}
Masonry——for循环,创建多个view视图。
- (void)addRightButtons:(NSArray<UIButton *> *)buttons
{
[self layoutIfNeeded];
[self setNeedsLayout];
UIButton *tempButton;
for (int i = 0; i < buttons.count; i++) {
UIButton *button = buttons[i];
button.tag = i;
[self addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(30, 30));
make.centerY.equalTo(self.backButton);
if (!tempButton) {
make.trailing.mas_equalTo(self.rightButton.mas_leading).inset(8);
} else {
make.trailing.mas_equalTo(tempButton.mas_leading).inset(8);
}
}];
tempButton = button;
}
[self.tempArrays appendObjects:buttons];
}
网友评论