Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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
Objective c 将背景图像设置为UITableView节标题_Objective C_Ios_Cocoa Touch_Uitableview - Fatal编程技术网

Objective c 将背景图像设置为UITableView节标题

Objective c 将背景图像设置为UITableView节标题,objective-c,ios,cocoa-touch,uitableview,Objective C,Ios,Cocoa Touch,Uitableview,我在网上和stackoverflow上搜索过,但找不到一个好的答案。 我读过 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section` 但是没有什么能帮到我 最接近标题图像背景的是: headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myBackground.p

我在网上和stackoverflow上搜索过,但找不到一个好的答案。 我读过

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section`

但是没有什么能帮到我

最接近标题图像背景的是:

headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myBackground.png"]];

但它将图片显示为一种模式,无法控制其大小

您可以将背景颜色设置为带有图像的图案:

headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]

如果要将图像设置为节标题,请使用:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIImage *myImage = [UIImage imageNamed:@"loginHeader.png"];
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
    imageView.frame = CGRectMake(10,10,300,100);

    return imageView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}

当然可以。在方法
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)部分
中,您只需使用图像实例化
UIImageView
,设置imageView对象的帧并返回imageView对象。

“但是没有任何东西可以帮助我。”-为什么不?当您实现第一个方法时会得到什么意外的结果?没有什么是意外的。我可以更改节头字体、颜色等,但不能使用图像作为节头背景。@MikaStem您阅读过一些文档吗
UIImage
不是
UIView
的子类,所以这很自然。你要找的课程名为
UIImageView
。有趣的是,7分钟内没有答案,然后我在评论中指出解决方案,然后在30秒内突然有两个答案。似乎是合法的…lazyness:P(我今天达到了代表上限)可以工作,但我已经从这个方法返回了
UIView
实例,其中包括我的标签对齐、字体等。如何返回图像并仍然保留标签?将标签添加为UIImageView的子视图您需要写一点,您的代码正在做什么,以及它如何解决问题。@EdgarKlerks谢谢,我的代码用于将图像n文本设置为节头,它最好与图像对齐。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];

    headerView.backgroundColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
    headerView.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
    headerView.layer.borderWidth = 1.0;

    UILabel* headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,headerView.frame.size.height-40, tableView.frame.size.width - 5, 30)];

    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:22.0];
    headerLabel.text = @"yourText";
    headerLabel.textAlignment = NSTextAlignmentLeft;

    UIImageView *headerImage = [[UIImageView alloc]initWithFrame:headerView.frame];


    headerImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"image.jpg"]];
    headerImage.contentMode = UIViewContentModeScaleAspectFill;
    [headerImage setClipsToBounds:YES];

    [headerView addSubview:headerImage];
    [headerView addSubview:headerLabel];

return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100;
}