Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将UISegmentedControl添加到带边距的UITableView的节标题中?_Ios_Objective C_Uitableview_Uikit - Fatal编程技术网

Ios 如何将UISegmentedControl添加到带边距的UITableView的节标题中?

Ios 如何将UISegmentedControl添加到带边距的UITableView的节标题中?,ios,objective-c,uitableview,uikit,Ios,Objective C,Uitableview,Uikit,我最近在tableview的一个节标题中添加了一个分段控件,一切正常,但它的大小调整错误。。我想应用一些边距,但是如果我设置框架,它对分段控件的大小没有任何影响?我做错了什么?这是我的密码: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if(section == 0) { UISegmentedControl *segmentedCon

我最近在tableview的一个节标题中添加了一个分段控件,一切正常,但它的大小调整错误。。我想应用一些边距,但是如果我设置框架,它对分段控件的大小没有任何影响?我做错了什么?这是我的密码:

- (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;
}

您返回的是UISegmentedControl实例,很明显,您无法在控件内部进行太多配置。相反,尝试创建一个UIView作为标题视图,并在其中添加分段控件作为子视图。通过这种方式,您将能够在这个容器视图中配置分段控件的位置

您返回的是UISegmentedControl实例,很明显,您无法在控件内部进行太多配置。相反,尝试创建一个UIView作为标题视图,并在其中添加分段控件作为子视图。通过这种方式,您将能够在这个容器视图中配置分段控件的位置

- (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;
}