ios - 如何将 UISegmentedControl 添加到带有边距的 UITableView 的部分标题?

标签 ios objective-c uitableview uikit

我最近在我的表格 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;
}

enter image description here

最佳答案

- (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/

相关文章:

ios - 动画完成后执行任务(在 iOS 中)

ios - 如何使用照片框架快速循环浏览照片库

iOS Mutual Auth 验证成功然后连接请求失败

objective-c - 在 Xcode 4.2 中找不到框架 header ,但应用程序运行正常

ios - imageWithContentsOfFile 不返回图像

ios - 交互关闭键盘时 UITableView contentInset 不更新

iphone - 选择 UITableViewCell 后如何延迟关闭模式窗口

ios - 自动布局和 Collection View 问题

ios - 样式 ActionSheet 的 UIAlertController 显示在状态栏下方

ios - LayoutSubviews 问题