开发者

change width of UIAlertView in iPad

开发者 https://www.devze.com 2022-12-29 16:02 出处:网络
Is there any way to change the frame of the UIAlertView in iPhone or iPad. I tried changing the frame in the following delegate method- (void)willPresentAlertView:(UIAlert开发者_开发技巧View *)alertVi

Is there any way to change the frame of the UIAlertView in iPhone or iPad. I tried changing the frame in the following delegate method- (void)willPresentAlertView:(UIAlert开发者_开发技巧View *)alertView;

but even then the width of the Alertview remained unchanged. And I think a small Alertview in iPad will not make sense. And I guess there must be a way to achieve it, at least in iPad.

Thanks, krishnan.


Hmm, this code works fine (except, when you are trying to decrease size - cause in this case you can get some bad rendering):

- (void)willPresentAlertView:(UIAlertView *)alertView {
    alertView.frame = CGRectMake(x, y, width, height);
}

Maybe, you forgot to set UIAlertView's delegate with something like someAlertView.delegate = self;

 

UPDATE ↓

codepart:

- (IBAction)go:(id)sender {
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:nil
        otherButtonTitles:nil] autorelease];
    alert.delegate = self;
    [alert show];
}

- (void)willPresentAlertView:(UIAlertView *)alertView {
    alertView.frame = CGRectMake(5.f, 1.f, 100.f, 200.f);
}

Result (on my iPod): http://dl.dropbox.com/u/6455784/alert_view_frame.png


this is the solution your looking for if you simply want to change the width of the standard UIAlertView with a large text field and one ok button. In my case I just wanted an alert with a large amount of text. And have that text all visible without having to scroll the textview. The trick is you have to change the width of all/some of the views inside. No guarantees this will pass app approval. They might find UIAlertTextView and reject it.

#define ALERT_VIEW_WIDTH 600
#define ALERT_PADDING 20
-(void) willPresentAlertView:(UIAlertView*) alertView
{
    id thing;
    int center=alertView.center.x-ALERT_VIEW_WIDTH/2;
    for (thing in alertView.subviews)
    {
        NSLog(@"%@", [[thing class] description]);
        if([[[thing class] description] isEqualToString:@"UIAlertTextView"])
        {
            UIView* v=(UIView*)thing;
            v.frame=CGRectMake(v.frame.origin.x+ALERT_PADDING, v.frame.origin.y, ALERT_VIEW_WIDTH-ALERT_PADDING*3, 250);
        }
        if([[[thing class] description] isEqualToString:@"UIThreePartButton"])
        {
            UIView* v=(UIView*)thing;
            v.frame=CGRectMake(v.frame.origin.x+ALERT_PADDING, v.frame.origin.y, ALERT_VIEW_WIDTH-ALERT_PADDING*3, v.frame.size.height);
        }
    }
    alertView.frame=CGRectMake(center, alertView.frame.origin.y, ALERT_VIEW_WIDTH, 360);

}


No textfields are present on my alertview, still when i set frame or use transform the view's background frame was rendered badly in the device. This thing will work fine in simulator only.


I know this is a old question but I was facing the same problem and the answers here helped me. I needed to enlarge the title and message text fields in order to accomodate more text on the iPad.

What I did is implement the UIAlertView willPresentAlertView: delegate method and add two UILabel objects to serve as title and message. I was than able to configure the frame size and origin of these two labels. After obtaining the desired size for the text fields I resize the alertview in order to accomodate the new text. Then I iterate through the subviews of the alertview to find the button and adjust it's frame.

- (void)willPresentAlertView:(UIAlertView *)alertView {

        // add a new label and configure it to replace the title
        UILabel *tempTitle = [[UILabel alloc] initWithFrame:CGRectMake(10,20,350, 20)];
        tempTitle.backgroundColor = [UIColor clearColor];
        tempTitle.textColor = [UIColor whiteColor];
        tempTitle.textAlignment = UITextAlignmentCenter;
        tempTitle.numberOfLines = 1;
        tempTitle.font = [UIFont boldSystemFontOfSize:18];
        tempTitle.text = alertView.title;
        alertView.title = @"";
        [alertView addSubview:tempTitle];
        [tempTitle release];

        // add another label to use as message 
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 350, 200)];
        tempLabel.backgroundColor = [UIColor clearColor];
        tempLabel.textColor = [UIColor whiteColor];
        tempLabel.textAlignment = UITextAlignmentCenter;
        tempLabel.numberOfLines = 20;
        tempLabel.text = alertView.message;
        [alertView addSubview:tempLabel];

        // method used to resize the height of a label depending on the text height
        [self setUILabel:tempLabel withMaxFrame:CGRectMake(10,50, 350, 300) withText:alertView.message];
        alertView.message = @"";

        // set the frame of the alert view and center it
        alertView.frame = CGRectMake(CGRectGetMinX(alertView.frame) - (370 - CGRectGetWidth(alertView.frame))/2 , 
                                     alertView.frame.origin.y, 
                                     370, 
                                     tempLabel.frame.size.height + 120);


        // iterate through the subviews in order to find the button and resize it
        for( UIView *view in alertView.subviews)
        {
            if([[view class] isSubclassOfClass:[UIControl class]])
            {
                view.frame = CGRectMake   (view.frame.origin.x+2,
                                           tempLabel.frame.origin.y + tempLabel.frame.size.height + 10,
                                           370 - view.frame.origin.x *2-4,
                                           view.frame.size.height);
            }
        }

        [tempLabel release];
}

And the method used for resizing the label:

    - (void)setUILabel:(UILabel *)myLabel withMaxFrame:(CGRect)maxFrame withText:(NSString *)theText{
    CGSize stringSize = [theText sizeWithFont:myLabel.font constrainedToSize:maxFrame.size lineBreakMode:myLabel.lineBreakMode];
    myLabel.frame = CGRectMake(myLabel.frame.origin.x, 
                               myLabel.frame.origin.y, 
                               myLabel.frame.size.width, 
                               stringSize.height
                               );
}

I don't know if this is the best way of doing this but it worked for me.


Use This Method with respective height and width for iPhone and iPad respectively:

- (void)willPresentAlertView:(UIAlertView *)alertView {
    [alertView setFrame:CGRectMake(5, 20, 300, 420)];
}


This small edit works for SWIFT 5:

let constraintHeight = NSLayoutConstraint(
          item: alertController.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
          NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 200)
alertController.view.addConstraint(constraintHeight)

let constraintWidth = NSLayoutConstraint(
          item: alertController.view!, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
          NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 400)
alertController.view.addConstraint(constraintWidth)
0

精彩评论

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