UITableViewHeaderFooterView的使用+自动布局

UITableViewHeaderFooterView的使用+自动布局

使用UITableView的header或footer复用时,如果采用自动布局,你会发现有约束冲突,下面这样写可以消除约束冲突:


#import <UIKit/UIKit.h>

@interface SectionView : UITableViewHeaderFooterView

@property (nonatomic, copy) NSString *sectionTitle;

@end



#import "SectionView.h"

@interface SectionView ()
{
    UIImageView *titleBgImageView;
    UIImageView *timePonitImageView;
    UIImageView *circleImageView;
    UILabel *titleLabe;
}
@end

@implementation SectionView

// 带有复用
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    return self;
}


- (void)createUI
{
    titleBgImageView = [[UIImageView alloc] initForAutoLayout];
    titleBgImageView.userInteractionEnabled = NO;
    UIImage *image = [UIImage imageNamed:@"event_bottom_line"];
    image = [image stretchableImageWithLeftCapWidth:image.size.width*0.5 topCapHeight:image.size.height*0.5];
    titleBgImageView.image = image;
    [self.contentView addSubview:titleBgImageView];
    
    
    circleImageView = [[UIImageView alloc] initForAutoLayout];
    UIImage *circleImage = [UIImage imageNamed:@"event_blue1"];
    circleImage = [circleImage stretchableImageWithLeftCapWidth:circleImage.size.width*0.5 topCapHeight:circleImage.size.height*0.5];
    
    circleImageView.image = circleImage;
    [self.contentView addSubview:circleImageView];

    
    timePonitImageView = [[UIImageView alloc] initForAutoLayout];
    timePonitImageView.image = [UIImage imageNamed:@"event_write_line"];
    [self.contentView addSubview:timePonitImageView];
    
    
    titleLabe = [[UILabel alloc] initForAutoLayout];
    titleLabe.font = [UIFont systemFontOfSize:13];
    titleLabe.textColor = [UIColor whiteColor];
    titleLabe.textAlignment = NSTextAlignmentCenter;
    [circleImageView addSubview:titleLabe];
   
}

把布局代码写到这里

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [titleBgImageView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    [circleImageView autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:8];
    [circleImageView autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:8];
    [circleImageView autoSetDimension:ALDimensionHeight toSize:23];
    [circleImageView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    
    [timePonitImageView autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:16];
    [timePonitImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:39/2.0-4];
    [timePonitImageView autoSetDimensionsToSize:CGSizeMake(9, 23.5)];
    
    [titleLabe autoCenterInSuperview];
    [titleLabe autoSetDimension:ALDimensionWidth toSize:200];
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值