Changing the background color of a UIAlertView?

前端 未结 12 1080
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 03:12

I want to change the background color of my UIAlertView, but this doesn\'t appear to have a color attribute.

相关标签:
12条回答
  • 2020-11-28 03:38

    Well i solved the problem in a different way. I searched for the UIImageView which contains the background image and change the image.

    ... well show is maybe not the best solution for altering the view ...

    @implementation CustomAlertView
    
    - (void)show {
        [super show];
    
        for (UIView *view in self.subviews) {
            NSLog(@"%@ with %i children", view, [view.subviews count]);
            // change the background image
            if ([view isKindOfClass:[UIImageView class]]) {
                UIImage *theImage = [UIImage imageNamed:@"password entry bg - default.png"];    
                ((UIImageView *)view).image = [theImage resizableImageWithCapInsets:UIEdgeInsetsMake(49, 8, 8, 8)];
            }
        }
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-28 03:40

    This is not something that is possible.

    Either you need to create your own alert-type view (including providing the animation, etc.) or use the default UIAlertView supplied by Apple.

    Apple tends to not expose things like the color of the UIAlertView so that the UI has a common feel across all applictions. Changing the color from app to app would be confusing to the user.

    0 讨论(0)
  • 2020-11-28 03:42

    Here's a much more recent solution that takes advantage of blocks and is modeled after TweetBot:

    https://github.com/Arrived/BlockAlertsAnd-ActionSheets

    0 讨论(0)
  • 2020-11-28 03:44

    Simply use this code,

     UIImage *theImage = [UIImage imageNamed:@"imageName.png"];
     UIView *view=[alert valueForKey:@"_backgroundImageView"];
     //Set frame according to adustable
      UIImageView *image=[[UIImageView alloc] initWithImage:theImage];
     [image setFrame:CGRectMake(0, 0, 280, 130)];
     [view addSubview:image];
    
    0 讨论(0)
  • 2020-11-28 03:52

    You have to use this:

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"You are Select Row of" message:@"Human" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            alert.backgroundColor=[UIColor redColor];
            [alert show];
            [alert release];
    

    it will changed the background color of UIAlertView.

    0 讨论(0)
  • 2020-11-28 03:53

    I recently needed this and ended up subclassing UIAlertView. Here is the code for anyone who is interested.

    http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color

    0 讨论(0)
提交回复
热议问题