[英]How to have both UILabel and UIButton items into a UIView for UITableView header section
This is my first post here, so I hope to do everything correctly.这是我在这里的第一篇文章,所以我希望一切都正确。
I'm trying to write a code in Objective-C that allows me to add a UIButton in a table header view, for a specific section.我正在尝试用 Objective-C 编写代码,允许我在表头视图中为特定部分添加 UIButton。
This is how it should look:这是它的外观:
I see correctly the title, but unfortunately the UIButton doesn't appear and I don't know why.. some months ago I've used a similar code and it worked, but after changing something due to another bug in the table view, it stopped working and unfortunately I haven't the original code anymore..我正确地看到了标题,但不幸的是 UIButton 没有出现,我不知道为什么......几个月前我使用了类似的代码并且它起作用了,但是由于表视图中的另一个错误而改变了一些东西,它停止工作,不幸的是我没有原始代码了..
Do you know where I'm wrong or do you know any alternative?你知道我错在哪里或者你知道任何替代方法吗?
This is the code in Objective-C (that should automatically align the title and the button, even when the user changes the device orientation and it should work for any screen size):这是 Objective-C 中的代码(它应该自动对齐标题和按钮,即使用户更改了设备方向并且它应该适用于任何屏幕尺寸):
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 2) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 0,0)];
[view setClipsToBounds:NO];
[view setPreservesSuperviewLayoutMargins:TRUE];
UILabel *textLabel = [[UILabel alloc] init];
textLabel.text = @"MY TITLE";
textLabel.textColor = [UIColor colorWithRed:0.427f green:0.427f blue:0.427f alpha:1.0f];
//[textLabel setFont:_tableViewFooterFont];
[textLabel setTextAlignment:4]; // Natural
[textLabel setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[textLabel setAccessibilityTraits:UIAccessibilityTraitHeader];
[view addSubview:textLabel];
UIButton *button = [UIButton buttonWithType:1];
//[button setHidden:FALSE];
//[button.titleLabel setFont:_tableViewFooterFont];
[button.titleLabel setTextColor:[UIColor redColor]];
[button setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[button setTitle:@"RESET" forState:0];
[button setContentCompressionResistancePriority:0 forAxis:UILayoutConstraintAxisHorizontal];
[button addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
NSDictionary *dictionary = NSDictionaryOfVariableBindings(textLabel, button);
NSLayoutConstraint *buttonConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[textLabel]->=0-[button]-|" options:0 metrics:NULL views:dictionary];
[view addConstraints:buttonConstraint];
NSLayoutConstraint *labelConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-12-[textLabel]-6-|" options:0 metrics:NULL views:dictionary];
[view addConstraints:labelConstraint];
NSLayoutConstraint *viewConstraint = [NSLayoutConstraint constraintWithItem:button attribute:10 relatedBy:0 toItem:textLabel attribute:10 multiplier:1.0 constant:0];
[view addConstraint:viewConstraint];
return view;
}
return NULL;
}
Try this:尝试这个:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create view for header
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)]; // set header view height as you want
headerView.backgroundColor = [UIColor clearColor] ;
// create label for header title
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width-130, 30)];
title.font = [UIFont fontWithName:@"FONT_NAME" size:16.0]; //optional
[title.heightAnchor constraintEqualToConstant:30].active = true;
[title.widthAnchor constraintEqualToConstant:tableView.frame.size.width-130].active = true;
// create button for header action
UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(self.yourTableView.frame.size.width-120, 0, 120, 30)];
[resetButton setTitle:@"RESET" forState:UIControlStateNormal];
//resetButton.backgroundColor = [UIColor redColor];
[resetButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[resetButton setFont:[UIFont fontWithName:@"FONT_NAME" size:16.0]] ;
[resetButton.heightAnchor constraintEqualToConstant:30].active = true;
[resetButton.widthAnchor constraintEqualToConstant:120].active = true;
//Stack View
UIStackView *stackView = [[UIStackView alloc] initWithFrame:headerView.frame];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFillProportionally;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 10;
[stackView addArrangedSubview:title];
[stackView addArrangedSubview:resetButton];
stackView.translatesAutoresizingMaskIntoConstraints = false;
[headerView addSubview:stackView];
if(section == 2){
title.text = @"MY TITLE";
[resetButton addTarget:self action:@selector(resetBtnActn:) forControlEvents:UIControlEventTouchUpInside] ;
}else{
headerView.hidden = YES ;
title.hidden = YES ;
resetButton.hidden = YES ;
}
//Trailing
NSLayoutConstraint *trailing =[NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.f];
//Leading
NSLayoutConstraint *leading = [NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeLeadingMargin
multiplier:1.0f
constant:0.f];
//Bottom
NSLayoutConstraint *bottom =[NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.f];
//Add constraints
[headerView addConstraint:trailing];
[headerView addConstraint:bottom];
[headerView addConstraint:leading];
// 5. Finally return
return headerView;
}
// table section button action
-(IBAction)resetBtnActn:(id)sender
{
NSLog(@"button tapped");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.