1

I first use " [UIAlertView alloc] initWithTitle: " to resize and customize AlertView with image but failed.

Then I use following codes, all works fine ( alertView resized and have a image with it), But just can't have a button to dismiss the alertView. Stuck here for half day. Please help, thanks!

UIAlertView* find = [[[UIAlertView alloc] init]initWithFrame:CGRectMake(0,0,300,420)];
    [find setDelegate:self];
    [find setTitle:@"Sample Alert"];
    [find setNeedsLayout];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 80, 80)];

NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"111.png"]];
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:bkgImg];
[bkgImg release];
[path release];

 [find addSubview:imageView];
[imageView release];

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

[find release];
Nayan
  • 3,014
  • 2
  • 17
  • 33
user1188849
  • 107
  • 1
  • 12

1 Answers1

2

UIAlertView was not made to be initialized in such a manner. Usually you initialize a UIAlertView using initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: .

You can try using [find addButtonWithTitle:@"button 1"]; . You will also need to implement the UIAlertView Delegate to interact with the button.

If that does not work, i recommend implementing your own custom view if you are looking for a heavily modified look.

hope this helps.

KDaker
  • 5,899
  • 5
  • 31
  • 44
  • Thanks KDaker so much! Only '[find addButtonWithTitle:@"button 1"];' is enough and works. It can dismiss AlertView. No need to implement the UIAlertView Delegate to interact with the button any more. – user1188849 Aug 25 '12 at 14:02
  • Thanks KDaker so much! Only '[find addButtonWithTitle:@"button 1"];' is enough and works. It can dismiss AlertView. No need to implement the UIAlertView Delegate to interact with the button any more. But there are 2 things unsatisfactory, one is that, the button is not align with the right and left edge of the alertview. The button's right edge is out of the right edge of the alert view. Although I resize and enlarge the alertView, the button is still out of the alertview. Is there a way to resize the button? Another thing unsatisfactory is that the alertView shakes once after it pops up. – user1188849 Aug 25 '12 at 14:22
  • UIAlertView does not give you control over it's button's frames. And i honestly would not recommend customizing UIAlertView too much because you could risk rejection from apple if you're submitting to the app store. Can you post a screenshot of what you have and what you're trying to achieve? – KDaker Aug 25 '12 at 20:25
  • So coincident that I also realized that I should have post a screenshot. Is it already too much that being risk of rejection by Apple so far ? Sorry, I tried to post a screenshot but failed. Please check the screenshot here, thanks! https://devforums.apple.com/message/719287 – user1188849 Aug 25 '12 at 22:50
  • well if you add a text message it would push the button down. But still i see what you are trying to achieve... Try initializing the alertView with `initWithTitle` the normal way and then just add your image as a subView : `[find addSubview:myImageView]`, you might need to place arbitrary text in the message just to resize the alertView. – KDaker Aug 25 '12 at 22:58
  • This is great! "add a text message" to resize the alert view is really a good method, also avoid the risk of rejection. I had used 'initWithTitle' before but just don't know this wise method, so have to go a long way as above posts. Although it still pop up not graceful with a shake, works anyway! Thanks KDaker again! – user1188849 Aug 25 '12 at 23:51
  • BTW I had wanted to use other method to achieve the effect but failed. So select AlerView. There are other posts suggest to use ' - (void)willPresentAlertView:(UIAlertView *)alertView { alertView.frame = CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y-80,alertView.frame.size.width , alertView.frame.size.height+130); }' , but my program just does not go into willPresentAlertView, even I set a point there. Don't know why. – user1188849 Aug 25 '12 at 23:56
  • were you setting the delegate to the alertView? – KDaker Aug 26 '12 at 00:16