gpt4 book ai didi

ios - 表头 View 自动布局问题

转载 作者:可可西里 更新时间:2023-11-01 04:41:16 30 4
gpt4 key购买 nike

我正在将自定义 UIView 的 XIB 文件加载为 View Controller 内的 uitableview 的标题 View 。

xib 文件的文件所有者是 View Controller 。我在 uiviewcontroller 中声明了 viewcontroller 和 uiview 的接口(interface)。

ViewController.h

      @class ZeroStateView;

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) IBOutlet CustomUITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet ZeroStateView *zeroStateView;


@end


@interface ZeroStateView : UIView
@property (weak, nonatomic) IBOutlet AutoLayoutLabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *titleIcon;

- (void)updateView;

@end

ViewController.m

    - (void)prepareHeaderViewForZeroState{
ZeroStateView *sizingView = [ZeroStateView new];
[[[NSBundle mainBundle] loadNibNamed:@"ZeroStateView" owner:self options:nil] objectAtIndex:0];
sizingView = self.zeroStateView;
[sizingView updateView];

self.tableView.tableHeaderView = sizingView;

UIView *headerView = self.tableView.tableHeaderView;



CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

CGRect headerFrame = headerView.frame;
headerFrame.size.height = height;
headerView.frame = headerFrame;


self.tableView.tableHeaderView = headerView;
}

@end

@implementation ZeroStateView

-(void)updateView{
self.titleIcon.alpha = 0.5;

UIFontDescriptor *titleFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleSubheadline];

self.titleLabel.text = @"This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. ";

}

AutolayoutLabel 类重写了以下方法:

   - (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];

// For multiline label, preferredMaxLayoutWidth always matches the frame width
if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) {
self.preferredMaxLayoutWidth = self.bounds.size.width;
[self setNeedsUpdateConstraints];
}
}

systemLayoutSizeFittingSize:UILayoutFittingCompressedSize 计算的高度返回 0。因此,我得到以下 View 作为表头 View : Header view with height 0

当我如下添加实际高度时,uilabel 溢出。我期望 uiview 随着标签高度的增长而增长。

 headerFrame.size.height = self.sizingView.frame.size.height;

TableHeaderView not being resized properly

这是 UIViews 约束的屏幕截图:

ZeroState view XIB file with constraints

我在这里错过了什么?有人可以指出我吗?

更新 我创建了一个示例 project让你们检查到底是什么问题。

最佳答案

我以不同的方式重新实现了您到目前为止的内容。首先,我删除了 ZeroStateView.xib 中嵌入了 UIImageViewUILabelUIView。 xib的已经是一个UIView,所以没有必要再添加一个UIView给它。

接下来我改变了周围的约束。我不记得具体更改了哪些约束,所以我将在此处列出它们:

  1. UIImageView 的约束
    • 将 X 中心与 SuperView 对齐
    • 宽度 = 60
    • 高度= 56
    • Superview 的顶部空间 = 37
    • UILabel 的底部空间 = 31
  2. UILabel 的约束
    • Superview 的剩余空间 = 15
    • Superview 的正确空间 = 15
    • 到 Superview 的底部空间 = 45
    • UIImageView 的顶部空间 = 31

进入代码。在 ViewController.h 中,据我所知,IBOutlet 没有做任何事情,所以我将该属性更改为读取 @property(强,非原子) ZeroStateView *zeroStateView;

现在是重要的变化:ViewController.m。有两个 UITableViewDelegate 方法将替换 prepareHeaderViewForZeroState。在 viewDidLoad 中,初始化 zeroStateView 并将 TableView 的委托(delegate)设置为 self

- (void)viewDidLoad
{
//...

// Load the view
self.zeroStateView = [[[NSBundle mainBundle] loadNibNamed:@"ZeroStateView" owner:self options:nil] firstObject];
[self.zeroStateView updateView];
self.zeroStateView.backgroundColor = [UIColor darkGrayColor];

// Set self for table view delegate for -heightForHeaderInSection: and viewForHeaderInSection:
self.dataTable.delegate = self;
}

现在我们是 TableView 的委托(delegate),我们得到两个方法调用,这将允许我们自定义标题 View 并适本地设置它的高度。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// This will set the header view to the zero state view we made in viewDidLoad
return self.zeroStateView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
// sizeToFit describes the size that the label can fit itself into.
// So we are saying that the label can use the width of the view and infinite height.
CGSize sizeToFit = CGSizeMake(self.view.frame.size.width, MAXFLOAT);

// Then we ask the label to tell us how it can fit in that size.
// The label will respond with the width of the view and however much height it needs
// to display its text. This is the magic of how it grows vertically.
CGSize size = [self.zeroStateView.titleLabel sizeThatFits:sizeToFit];

// Add the height the label needs to the overall zero state view. This should be changed
// to the height of the UIImage + the height we just got + the whitespace above and below
// each of these views. You can handle that part.
return self.zeroStateView.frame.size.height + size.height;
}

我将更改上传到 Dropbox here .

关于ios - 表头 View 自动布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995541/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com