有什么方法可以更改 iPhone 或 iPad 中 UIAlertView 的框架。我尝试在以下委托方法中更改框架- (void)willPresentAlertView:(UIAlertView *)alertView;
但即便如此,Alertview 的宽度仍然保持不变。而且我认为 iPad 中的小型 Alertview 没有意义。而且我想一定有办法实现它,至少在 iPad 中是这样。
谢谢,克里希南。
有什么方法可以更改 iPhone 或 iPad 中 UIAlertView 的框架。我尝试在以下委托方法中更改框架- (void)willPresentAlertView:(UIAlertView *)alertView;
但即便如此,Alertview 的宽度仍然保持不变。而且我认为 iPad 中的小型 Alertview 没有意义。而且我想一定有办法实现它,至少在 iPad 中是这样。
谢谢,克里希南。
嗯,这段代码工作正常(除非你试图减小尺寸——因为在这种情况下你可能会得到一些糟糕的渲染):
- (void)willPresentAlertView:(UIAlertView *)alertView {
alertView.frame = CGRectMake(x, y, width, height);
}
也许,您忘记了 setUIAlertView
的委托,例如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
如果您只想使用大文本字段和一个确定按钮更改标准 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);
}
我的警报视图上没有文本字段,但当我设置框架或使用变换时,视图的背景框架在设备中渲染得很糟糕。这东西只能在模拟器中正常工作。
我知道这是一个老问题,但我遇到了同样的问题,这里的答案帮助了我。我需要放大标题和消息文本字段以便在 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
);
}
我不知道这是否是最好的方法,但它对我有用。
使用此方法分别为 iPhone 和 iPad 的高度和宽度:
- (void)willPresentAlertView:(UIAlertView *)alertView {
[alertView setFrame:CGRectMake(5, 20, 300, 420)];
}