【问题标题】:change width of UIAlertView in iPad在 iPad 中更改 UIAlertView 的宽度
【发布时间】:2010-05-04 08:04:58
【问题描述】:

有什么方法可以改变 iPhone 或 iPad 中 UIAlertView 的框架。我尝试在以下委托方法中更改框架- (void)willPresentAlertView:(UIAlertView *)alertView;

但即便如此,Alertview 的宽度仍然保持不变。而且我认为 iPad 中的小型 Alertview 没有意义。而且我想一定有办法实现它,至少在 iPad 中是这样。

谢谢, 克里希南。

【问题讨论】:

  • 你有没有找到办法做到这一点?只是好奇,这里有很多答案,三年过去了......等等。

标签: iphone uialertview


【解决方案1】:

嗯,这段代码可以正常工作(除了,当你试图减小尺寸时——因为在这种情况下你可能会得到一些糟糕的渲染):

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

也许,您忘记将 UIAlertView 的委托设置为 someAlertView.delegate = self; 之类的东西

 

更新↓

代码部分:

- (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);
}

结果(在我的 iPod 上):http://dl.dropbox.com/u/6455784/alert_view_frame.png

【讨论】:

  • 我确实设置了委托。 AlertView 的高度发生了变化,但宽度没有变化。它真的对你有用吗?
  • 是的。但是,当视图的宽度小于正确显示(渲染)文本所需的宽度时,视图的背景框架被渲染得很糟糕(框架矩形的左边部分是圆角的,但右边不是)。
  • 您能确认您的解决方案有效吗? astalapista 说它不起作用。
【解决方案2】:

如果您只想使用大文本字段和一个确定按钮更改标准 UIAlertView 的宽度,这就是您正在寻找的解决方案。就我而言,我只是想要一个带有大量文本的警报。并且无需滚动文本视图即可使该文本全部可见。 诀窍是您必须更改内部所有/某些视图的宽度。 不保证这将通过应用程序批准。他们可能会找到 UIAlertTextView 并拒绝它。

#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);

}

【讨论】:

  • 您使用的应用是否获得批准?
【解决方案3】:

我的警报视图上没有文本字段,但当我设置框架或使用变换时,视图的背景框架在设备中呈现得很糟糕。这东西只能在模拟器中正常工作。

【讨论】:

    【解决方案4】:

    我知道这是一个老问题,但我遇到了同样的问题,这里的答案对我有帮助。 我需要放大标题和消息文本字段以便在 iPad 上容纳更多文本。

    我所做的是实现 UIAlertView willPresentAlertView: 委托方法并添加两个 UILabel 对象作为标题和消息。我能够配置这两个标签的帧大小和原点。在获得文本字段的所需大小后,我调整了警报视图的大小以适应新文本。然后我遍历alertview的子视图以找到按钮并调整它的框架。

    - (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];
    }
    

    以及用于调整标签大小的方法:

        - (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
                                   );
    }
    

    我不知道这是否是最好的方法,但它对我有用。

    【讨论】:

    • 是的,在 ios7 之前它可以工作,但对于 IOS 7,它不能工作。
    【解决方案5】:

    将此方法分别用于 iPhone 和 iPad 的高度和宽度:

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

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多