2010-10-20 7 views
9

Bonjour Je reçois un alertview et je veux changer la taille par la propriété cgrectmake mais cela n'arrive pas. Il suffit de prendre la taille par défaut.problème en changeant la taille de uialertview

J'ai essayé le code suivant.

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

}

Merci à l'avance.

+0

Même problème pour moi. Je ne peux pas changer la largeur et la hauteur de UIAlertView. De l'aide? Merci d'avance – Sakthimuthiah

Répondre

16

Pour obtenir ce que vous voulez, dans votre code, après:

[find show]; 

ajouter:

find.frame = CGRectMake(0,0,300,200); 

Ce n'est pas assez bien, je vous suggère d'utiliser ActionSheets.

+0

ici je veux changer la largeur de la vue d'alerte comment puis-je changer cela, je suis capable de changer la hauteur, mais ne sais pas comment changer la largeur. – mrugen

+0

Eh bien ma réponse s'applique encore, vous pouvez changer la largeur après l'appel de [trouver], et le résultat est encore moche et vous devez toujours utiliser des feuilles d'actions –

+0

cela change juste le uiwindow pour moi (ie - le textfield 'message' inside doesn ' t resize) – roocell

3

Vous pouvez sous-classer UIAlertView. J'ai fait quelque chose comme ça, changez-le pour votre besoin.

fichier d'en-tête,

#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 

code source,

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

Ce code fait beaucoup de choses à alertview et le modifier pour augmenter la taille de l'affichage des alertes.

7

Cela fait le travail assez bien, et n'a pas l'air bizarre lorsque l'alerte apparaît (ahmet emrah solution a un effet secondaire assez mauvais).

CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f); 
[alert setTransform:myTransform]; 
+0

Non. Cela réduit le contenu de la vue d'alerte, déformant le texte et tout. – AWrightIV

+0

Merci mec !!!!! – Nishant

+0

Génial. Fonctionne très bien. –