[英]Custom table section footer view - label not visible
I created a new Single-view project (using storyboard) in Xcode with a single UITableViewController. 我用一个UITableViewController在Xcode中创建了一个新的Single-view项目(使用情节提要)。 Here is the setup code: 这是设置代码:
- (void)viewDidLoad {
[super viewDidLoad];
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 44, 44)];
l.text = @"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return _footerView.frame.size.height;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return _footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView dequeueReusableCellWithIdentifier:@"Cell"];
}
I want the label in the custom table footer view to be drawn at x = 60, but when I run the project, at first the label is invisible (in portrait, screen attached). 我希望在自定义表格页脚视图中以x = 60绘制标签,但是当我运行项目时,起初标签是不可见的(纵向,附有屏幕)。 Then if I rotate once, it becomes visible and if I rotate back to portrait it's visible. 然后,如果我旋转一次,它将变为可见;如果我旋转回纵向,则它将变为可见。
What am I missing? 我想念什么?
You seem to be initialising your footer view with a width and height of 44px, yet adding the label outside of its bounds. 您似乎正在以44px的宽度和高度初始化页脚视图,但在其边界之外添加标签。
Try the following instead: 请尝试以下操作:
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectInset(_footerView.bounds, 60.0f, 0.0f)];
l.text = @"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
As a further point, try not to use [UIColor clearColor]
as a label background colour if you can help it - it significantly reduces scrolling performance. 此外,如果可以帮助,请尽量不要使用[UIColor clearColor]
作为标签背景色-这会大大降低滚动性能。 In this case you should use [UIColor lightGrayColor]
so it matches its superview. 在这种情况下,您应该使用[UIColor lightGrayColor]
使其匹配其[UIColor lightGrayColor]
视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.