我最近在我的表格 View 的部分标题中添加了一个分段控件,一切正常,但它调整大小的方式错误..我想应用一些边距,但如果我设置框架它对大小没有任何影响分段控制 ?我做错了什么?这是我的代码:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section == 0) {
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Segment 1", @"Segment 2",@"Segment 3"]];
segmentedControl.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.95];
[segmentedControl setFrame:CGRectMake(10, 0, self.view.bounds.size.width-10, self.view.bounds.size.height)];
return segmentedControl;
}
return nil;
}
最佳答案
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section == 0) {
UIView * viewHeader = [[UIView alloc]initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width-10, self.view.bounds.size.height)];
[viewHeader setBackgroundColor:[UIColor clearColor]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Segment 1", @"Segment 2",@"Segment 3"]];
segmentedControl.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.95];
[segmentedControl setFrame:CGRectMake(10, 0, viewHeader.frame.size.width , viewHeader.frame.size.height)];
viewHeader addSubview:segmentedControl
return viewHeader;
}
return nil;
}
关于ios - 如何将 UISegmentedControl 添加到带有边距的 UITableView 的部分标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27641745/