Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.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 在Xib中以编程方式设置背景图像_Ios_Cocoa Touch_Uiview_Uiimageview_Uicontrol - Fatal编程技术网

Ios 在Xib中以编程方式设置背景图像

Ios 在Xib中以编程方式设置背景图像,ios,cocoa-touch,uiview,uiimageview,uicontrol,Ios,Cocoa Touch,Uiview,Uiimageview,Uicontrol,我有一个包含UIControl和UIScrollView元素的XIB文件。我想在视图中添加背景图像。我尝试在IB中添加一个ImageView,但我无法将其作为背景显示,它模糊了控制元素。发送sendViewBack消息似乎也没有任何作用 当我以编程方式创建UIImageView时,它不会显示 下面是我尝试的代码: 程序创建 UIImage *imageBackground = [UIImage imageWithContentsOfFile:@"globalbackground"]; UIIm

我有一个包含
UIControl
UIScrollView
元素的XIB文件。我想在视图中添加背景图像。我尝试在IB中添加一个ImageView,但我无法将其作为背景显示,它模糊了控制元素。发送
sendViewBack
消息似乎也没有任何作用

当我以编程方式创建UIImageView时,它不会显示

下面是我尝试的代码:

程序创建

UIImage *imageBackground = [UIImage imageWithContentsOfFile:@"globalbackground"];

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:imageBackground];

[[self view] addSubview:backgroundView];

[[self view] sendSubviewToBack:backgroundView];
处理NIB文件

[[self view] sendSubviewToBack:background];
其中背景是头文件中声明的并连接到IB中NIB图像视图的
IBOutlet


这里是否缺少一个步骤?

设置帧,不要使用
发送子视图备份:
。如果使用UIImageView,则必须使用
[[UIImageView alloc]initWithImage:[UIImage imageName:@“imageName.png”]

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageBackground"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];
希望这就是交易。

  • 不要将图像视图添加为滚动视图的子视图,它需要是层次结构顶层的单独视图,然后发送到Z顺序的后面
  • 您需要将滚动视图的背景设置为
    [UIColor clearColor]
    ,并确保滚动视图未标记为不透明。您可以在代码或interface builder中执行此操作
  • 不要使用
    imageWithContentsOfFile
    ,然后只传递一个没有扩展名的文件名(我假设为.png)——这可能会返回
    nil
    。改用
    imageNamed:
    (在这种情况下,您不提供扩展,iOS4及更高版本)
根据图像的性质,您还可以使用图像生成颜色,并将其用作滚动视图的背景色。我假设self.view是滚动视图:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"globalBackground"]];

如果还有其他UI组件,请确保使用insertSubView,如下所示:[self.view insertSubView:backgroundView atIndex:0];