[英]How to center a UIButton that is a header to a section in a UITableView?
[英]How to have both UILabel and UIButton items into a UIView for UITableView header section
這是我在這里的第一篇文章,所以我希望一切都正確。
我正在嘗試用 Objective-C 編寫代碼,允許我在表頭視圖中為特定部分添加 UIButton。
這是它的外觀:
我正確地看到了標題,但不幸的是 UIButton 沒有出現,我不知道為什么......幾個月前我使用了類似的代碼並且它起作用了,但是由於表視圖中的另一個錯誤而改變了一些東西,它停止工作,不幸的是我沒有原始代碼了..
你知道我錯在哪里或者你知道任何替代方法嗎?
這是 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;
}
嘗試這個:
-(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.