xcode 改变uialertview大小的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3975347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem in changing size of uialertview
提问by mrugen
Hello I am having a alertview and i want to change the size by cgrectmake property but it does not happen . It just take the by default size.
您好,我有一个警报视图,我想通过 cgrectmake 属性更改大小,但它没有发生。它只是采用默认大小。
i tried following code.
我尝试了以下代码。
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView* find = [[[UIAlertView alloc] init]initWithFrame:CGRectMake(0,0,300,200)];
[find setDelegate:self];
[find setTitle:@"Sample Alert"];
[find setNeedsLayout];
[find show];
[find release];
}
}
Thanks in advance .
提前致谢 。
回答by ahmet emrah
To achieve what you want, in your code, after:
为了实现你想要的,在你的代码中,之后:
[find show];
add:
添加:
find.frame = CGRectMake(0,0,300,200);
It's not pretty though, I suggest you use ActionSheets.
不过它并不漂亮,我建议您使用 ActionSheets。
回答by Pankaj Kainthla
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@" Submit the answer " delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];
[alert release];
UILabel *theTitle = [alert valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor blackColor]];
UILabel *theBody = [alert valueForKey:@"_bodyTextLabel"];
[theBody setTextColor:[UIColor blackColor]];
UIImage *theImage = [UIImage imageNamed:@"blue-white-abstract-background.jpg"];
theImage = [theImage stretchableImageWithLeftCapWidth:10 topCapHeight:10];
CGSize theSize = [alert frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[[alert layer] setContents:[theImage CGImage]];
[[alert layer] setContents:[UIColor clearColor]];
This code does a lot of things to alertview and modify it to increase the size of the alert view.
这段代码对alertview做了很多事情,并修改它以增加alert view的大小。
回答by legz
This does the job pretty well, and does not look weird when the alert appear (ahmet emrah solution has a pretty bad side effect).
这可以很好地完成工作,并且在出现警报时看起来并不奇怪(ahmet emrah 解决方案具有非常糟糕的副作用)。
CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f);
[alert setTransform:myTransform];
回答by karim
You can subclass the UIAlertView. I have done something like this, change it for your need.
您可以子类化 UIAlertView。我做过这样的事情,根据你的需要改变它。
Header file,
头文件,
#import <Foundation/Foundation.h>
/* An alert view with a textfield to input text. */
@interface AlertPrompt : UIAlertView
{
UITextField *textField;
}
@property (nonatomic, retain) UITextField *textField;
@property (readonly) NSString *enteredText;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
@end
Source code,
源代码,
#import "AlertPrompt.h"
@implementation AlertPrompt
static const float kTextFieldHeight = 25.0;
static const float kTextFieldWidth = 100.0;
@synthesize textField;
@synthesize enteredText;
- (void) drawRect:(CGRect)rect {
[super drawRect:rect];
CGRect labelFrame;
NSArray *views = [self subviews];
for (UIView *view in views){
if ([view isKindOfClass:[UILabel class]]) {
labelFrame = view.frame;
} else {
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + kTextFieldHeight , view.frame.size.width, view.frame.size.height);
}
}
CGRect myFrame = self.frame;
self.textField.frame = CGRectMake(95, labelFrame.origin.y+labelFrame.size.height + 5.0, kTextFieldWidth, kTextFieldHeight);
self.frame = CGRectMake(myFrame.origin.x, myFrame.origin.y, myFrame.size.width, myFrame.size.height + kTextFieldHeight);
}
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{
if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
// add the text field here, so that customizable from outside. But set the frame in drawRect.
self.textField = [[UITextField alloc] init];
[self.textField setBackgroundColor:[UIColor whiteColor]];
[self addSubview: self.textField];
// CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 20.0);
// [self setTransform:translate];
}
return self;
}
- (void)show
{
[textField becomeFirstResponder];
[super show];
}
- (NSString *)enteredText
{
return textField.text;
}
- (void)dealloc
{
[textField release];
[super dealloc];
}
@end