开发者

Put a UIImageView in the background

开发者 https://www.devze.com 2023-03-16 23:18 出处:网络
Here is my code : - (void)viewDidLoad { [super viewDidLoad]; int rand2 = arc4random() % 5; switch (rand2) {

Here is my code :

- (void)viewDidLoad {
  [super viewDidLoad];
  int rand2 = arc4random() % 5;
  switch (rand2) {
  case 0: 
    imgView =   [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pinkfont.png"]];          
    break;
  case 1:
imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greenfont.png"]];
        break;
  case 2:
    imgView =  [[UIImageView alloc] initWith开发者_运维问答Image:[UIImage imageNamed:@"whitefont.png"]];
    break;
  case 3:
    imgView =  [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellowfont.png"]];
    break;
  case 4:
    imgView =  [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"purplefont.png"]];
    break;
   }


   [[[self view] layer] addSublayer:[imgView layer]];
   [imgView release];

}

I want to put imgView at the background, but it doesn't work, I tried to do it with interface Builder with "send to back" but no result. How can I solve this please?

Sorry for my english I'm french :)


if you have xib file then drag a image view in your xib file and then from menu tab select layoutand choose sent to back or backward. or you have to create dynamically then write

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


you can do:

[self.view addSubview:imgView];

[self.view sendSubviewToBack:imgView];

Check after this code, you are not adding other views in viewWillAppear, if you add other views, send subview to back again.


write this line of code at the end of your viewwillappear method.

[self.view sendSubviewToBack:imgView];


to set image as your view background use this

UIImage *img = [UIImage imageNamed:@"pinkfont.png"]
[self.view setBackgroundColor:[UIColor colorWithPatternImage:img]];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号