2010-10-20 8 views
9

Ciao Sto avendo una vista di avviso e voglio cambiare la dimensione dalla proprietà cgrectmake ma non succede. Prende solo la dimensione predefinita.problema nella modifica della dimensione di uialertview

ho provato il seguente codice.

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

}

Grazie in anticipo.

+0

Stesso problema per me. Non riesco a cambiare larghezza e altezza di UIAlertView. Qualsiasi aiuto? Grazie in anticipo – Sakthimuthiah

risposta

16

per ottenere quello che vuoi, nel codice, dopo:

[find show]; 

aggiuntivo:

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

Non è abbastanza, però, ti suggerisco di usare ActionSheets.

+0

qui voglio cambiare la larghezza della vista di avviso come posso cambiarlo, sono in grado di cambiare l'altezza ma non so come cambiare la larghezza. – mrugen

+0

Bene la mia risposta si applica ancora è possibile modificare la larghezza dopo la chiamata a [trova spettacolo], e il risultato è ancora brutto e si continua a usare i fogli di azioni –

+0

questo cambia solo la finestra di dialogo per me (cioè - il campo di testo 'dentro' doesn ' t ridimensionare) – roocell

3

È possibile creare sottoclasse di UIAlertView. Ho fatto qualcosa di simile, lo cambio per il tuo bisogno.

file di intestazione,

#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 

codice sorgente,

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

Questo codice fa un sacco di cose da alertview e modificarlo per aumentare la dimensione della visualizzazione degli avvisi.

7

Questo funziona abbastanza bene, e non sembra strano quando compare l'avviso (la soluzione ahmet emrah ha un effetto collaterale piuttosto negativo).

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

No. Questo riduce il contenuto della vista di avviso, distorcendo il testo e tutto. – AWrightIV

+0

Grazie signore !!!!! – Nishant

+0

Fantastico. Funziona molto bene. –