2012-02-14 81 views
3

我有一個UIAlertView與一個圖像和一個大按鈕。如何設置UIAlertView的高度和寬度?

問題是,默認UIAlertView是一個非常小的尺寸。它的圖像無法正常顯示。

所以,我想增加UIAlertView的高度和寬度。換句話說,我想創建一個自定義UIAlertView。將有什麼辦法做到這一點?

謝謝。

+0

你可以讓警報視圖吃很多東西來增加體重嗎? ;) – 2012-02-14 10:20:59

+0

易於使用的方法:在郵件正文 – basvk 2012-02-14 10:22:24

+0

@ basvk-thaks上附加/附加額外的換行符(\ n)用於重播。但它只會增加alertview的高度,而不是寬度。主要的是我想顯示圖像而不是消息。 – Rohan 2012-02-14 10:29:38

回答

6

我向你發送我的代碼,我在AlertView中顯示了一個TableView,它可能會幫助你。

在頭文件中粘貼這段代碼。

#import <UIKit/UIKit.h> 

@interface aaViewController : UIViewController <UIAlertViewDelegate, UITableViewDelegate, UITableViewDataSource>{ 

    UITextField *textfield; 
    UITableView *tableView1; 

    NSMutableArray *_data; 
} 

@property (nonatomic, retain) NSMutableArray *_data; 

- (void) showAlertView; 
- (void) doAlertViewWithTextView; 
- (void) doAlertViewWithTableView; 
@end 

並在您的imlpementation(.m)文件中粘貼此代碼。

#import "aaViewController.h" 

@implementation aaViewController 

@synthesize _data; 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self._data = [NSMutableArray arrayWithObjects:@"Najeeb", @"Furqan", @"Khalid", nil]; 
    [self doAlertViewWithTableView]; 
} 

// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return YES; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

- (void) doAlertViewWithTextView { 

    UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 300, 550)]; 

    alert.title = @"Textview"; 
    alert.message = @"I am doing cool stuff\n\n\n"; 
    alert.delegate = self; 
    [alert addButtonWithTitle:@"Cancel"]; 
    [alert addButtonWithTitle:@"OK"]; 

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(12, 90, 260, 200)]; 
    textView.text = @"This is what i am trying to add in alertView.\nHappy New Year Farmers! The new Winter Fantasy Limited Edition Items have arrived! Enchant your orchard with a Icy Peach Tree, and be the first farmer among your friends to have the Frosty Fairy Horse. Don't forget that the Mystery Game has been refreshed with a new Winter Fantasy Animal theme! "; 
    textView.keyboardAppearance = UIKeyboardAppearanceAlert; 

    [alert addSubview:textView]; 
    [textView release]; 

    [alert show]; 
    [alert release]; 
} 

- (void) doAlertViewWithTableView { 

    NSLog(@"Now play with TableView \n%@", self._data); 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Table View" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

    tableView1 = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 200) style:UITableViewStyleGrouped] autorelease]; 
    tableView1.dataSource = self; 
    tableView1.delegate = self; 
    [alert addSubview:tableView1]; 

    [alert show]; 
    [alert release]; 
} 

//#define kTag_EnterNameAlert 1 
//#define kTag_NameEmtryField 100 
- (void) showAlertView { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congratulations!" 
                message:@"You earned a top score! Enter your name:\n\n" 
                delegate:self 
              cancelButtonTitle:nil 
              otherButtonTitles:@"OK", nil]; 

    //alert.tag = kTag_EnterNameAlert; 

    CGRect entryFieldRect = CGRectZero; 
    if(UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) { 
     entryFieldRect = CGRectMake(12, 90, 260, 25); 
    } 
    else { 
     entryFieldRect = CGRectMake(12, 72, 260, 25); 
    } 
    textfield = [[UITextField alloc] initWithFrame:entryFieldRect]; 
    //textfield.tag = kTag_NameEmtryField; 
    textfield.backgroundColor = [UIColor whiteColor]; 
    textfield.keyboardType = UIKeyboardTypeAlphabet; 
    textfield.keyboardAppearance = UIKeyboardAppearanceAlert; 
    textfield.autocorrectionType = UITextAutocorrectionTypeNo; 
    textfield.clearButtonMode = UITextFieldViewModeWhileEditing; 
    [alert addSubview:textfield]; 
    [textfield becomeFirstResponder]; 
    [textfield release]; 

    [alert show]; 
    [alert release]; 
} 

# pragma - 
# pragma alertView frame Methods 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    NSLog(@"name:%@ buttonID:%d",textfield.text,buttonIndex); 
    if (buttonIndex == 1) { 
     [self doAlertViewWithWebView]; 
    } 
    else { 
     NSLog(@"%@",alertView.message); 
    } 
} 

// to set the alertView frame size. 
- (void)willPresentAlertView:(UIAlertView *)alertView { 

    [alertView setFrame:CGRectMake(10, 100, 300, 320)]; 
    for (UIView *views in [alertView subviews]) { 
     NSLog(@"%@",views); 
     if (views.tag == 1 || views.tag == 2) { 
      [views setFrame:CGRectMake(views.frame.origin.x, views.frame.origin.y+200, views.frame.size.width, views.frame.size.height)]; 
     } 
    } 
} 

# pragma - 
#pragma mark TableView Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 1) { 
     return [_data count]; 
    } 
    else 
    return 3; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString* const SwitchCellID = @"SwitchCell"; 
    UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID]; 
    if(aCell == nil) { 
     aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease]; 
     aCell.textLabel.text = [NSString stringWithFormat:@"Option %d", [indexPath row] + 1]; 
     aCell.selectionStyle = UITableViewCellSelectionStyleNone; 
     UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     switchView.tag = [indexPath row]+1; 
     aCell.accessoryView = switchView; 
     [switchView setOn:YES animated:NO]; 
     [switchView addTarget:self action:@selector(soundSwitched:) forControlEvents:UIControlEventValueChanged]; 
     [switchView release]; 
    } 

    return aCell; 
} 

- (void) soundSwitched:(UISwitch*) switchView { 

    if (!switchView.on) { 
     NSLog(@"chal bhag %d", switchView.tag); 
    } 
} 

@end 

注意:在你的AppDelegate中添加子視圖到這個。