Custom UIAlertView iphone

2019-06-11 07:48发布

问题:

I have a problem .. I used this http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color to create my own custom UIAlertView. I do not know why but this will not work:

UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];

[theTitle setTextColor:[UIColor redColor]];

UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];

[theBody setTextColor:[UIColor blueColor]];

the color of the title does not change .. the color of texbody it's ok.

  1. How can I customize the buttons?

回答1:

Hi Achieved same thing using custom UIAleartView.

  1. Make a custom view as follows.

    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 5.0f, 262.0, 49)]; 
    tempView.backgroundColor = [UIColor clearColor];
    
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 262.0, 49)];
    lblTitle.font = [UIFont fontWithName:@"Arial" size:22.0f];
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.textAlignment = UITextAlignmentCenter;
    lblTitle.text = @"Subscribe";
     lblTitle.backgroundColor = [UIColor clearColor];
     [tempView addSubview:lblTitle];
    
    alreadySubscriber = [[UIButton alloc] initWithFrame:CGRectMake(12, 260, 262.0, 50)];
    alreadySubscriber.layer.cornerRadius = 25.0f;
    [alreadySubscriber setTitle:@"Already a subscriber" forState:UIControlStateNormal];
    [alreadySubscriber setBackgroundImage:[UIImage imageNamed:@"BTN0.png"] forState:UIControlStateNormal];
    [alreadySubscriber setBackgroundImage:[UIImage imageNamed:@"BTN1.png"] forState:UIControlStateSelected];
    [tempView addSubview:alreadySubscriber];
    
  2. Insert this in UIAleartView

    [self insertSubview:tempView atIndex:0];
    [self setNeedsLayout];
    
  3. Override layoutsubview as you have already done push all other controls down equal to view Height.

-> basically matter is to hide UIAleartView's title behind a label.



回答2:

Not sure exactly what your issue might be, but we faced a similar situation when trying to workout a custom UIAlertView, so might be similar.

The custom solution in the link you provided appears to manipulate the alerts title and background by accessing the subview hierarchy and 'guessing' which subview might be which. (I may be wrong, didn't look through it in detail) The problem with this approach is that it'll work fine for one OS version, but in subsequent OS versions, Apple may restructure this subview hierarchy in some manner, and this 'guesswork' is no longer accurate. (i.e. the subview assumed to be the background image may not be).

This could be the case, seeing that posted link is an year old. If you're proceeding with this, you may have to review the subview hierarchy to see if they still match up.



回答3:

I believe, that you can find proper solution without using standart tools, which are present in UIAlertView. But in this case, you application will be not approve for AppStore. That way, I strongly recommend you, avoid to using custom buttons in AlertView. Maybe, you will find solution using UIActionSheet instead UIAlertView . It's more customizing.



回答4:

If @"_titleLabel" is part of the UIAlertView hierarchy (which looks likely given the _ prefix), I can't recommend your approach.

Apple might some day change the key strings: If @"_titleLabel" ever changes, say to @"_titleLabelView", you're sunk and you might never know that you're sunk. This might even be grounds for rejection, I wouldn't know.

It's better to start out from scratch with your own custom view, subclassing UIView. Only then can you guarantee that this will be stable from OS to OS. On top of this, the time you lose trying to find a shortcut will be positively spent constructing some thing durable.