Wednesday, June 12, 2013

UIActionSheet willPresentActionSheet example in Objective C (iOS).

UIActionSheet willPresentActionSheet

Sent to the delegate before an action sheet is presented to the user.

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet

Parameters of [UIActionSheet willPresentActionSheet]
The action sheet that is about to be displayed.

UIActionSheet willPresentActionSheet example.
I assume you have a class which implements the UIActionSheetDelegate protocol and your class is a delegate class of the current UIActionSheet, so in that class you can change the whole title of the UIActionSheet like

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    for (UIView *_currentView in actionSheet.subviews) {
        if ([_currentView isKindOfClass:[UILabel class]]) {
            [((UILabel *)_currentView) setFont:[UIFont boldSystemFontOfSize:15.f]];

Example of [UIActionSheet willPresentActionSheet].
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{

UIImageView* df = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourpic.png"]];

[actionSheet addSubview:df];
//scale image aspect to fit image view
df.contentMode = UIViewContentModeScaleAspectFit;
//change width of frame
CGRect frame = df.frame;
frame.size.width = 50;
df.frame = frame;

UIActionSheet willPresentActionSheet example.
- (void)notifyShowSuccessActionSheet:(NSNotification *) notification {

NSLog(@\"In ETITMainViewController notifyShowSuccessActionSheet\n\");

UIActionSheet *successActionSheet = [[[UIActionSheet alloc] initWithTitle:nil
   otherButtonTitles:@\"Do More\", @\"Done\", @\"Another Test?\", @\"Yet Another\", nil] autorelease];
[successActionSheet setOpaque:NO];
[successActionSheet setAlpha:0.8];
[successActionSheet showFromToolbar:[[self naviController] toolbar]];


...and in willPresentActionSheet:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {

UIImageView* successImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"CheckMark64.png\"]];
NSInteger itemPadding;
NSInteger topPadding = [[[actionSheet subviews] objectAtIndex:0] frame].origin.y;

if ([[actionSheet subviews] count] > 1) {
itemPadding = [[[actionSheet subviews] objectAtIndex:1] frame].origin.y
- ([[[actionSheet subviews] objectAtIndex:0] frame].origin.y
   + [[[actionSheet subviews] objectAtIndex:0] frame].size.height);
else {
itemPadding = [[[actionSheet subviews] objectAtIndex:0] frame].size.height / 4;

// resize action sheet frame to make space for image
[actionSheet setFrame:CGRectMake([actionSheet frame].origin.x,
 [actionSheet frame].origin.y,
 [actionSheet frame].size.width,
 [actionSheet frame].size.height + [successImageView frame].size.height + itemPadding)];

 // re-position buttons
 for (UIControl *button in [actionSheet subviews]) {

[button setFrame:CGRectMake([button frame].origin.x,
[button frame].origin.y + [successImageView frame].size.height + itemPadding,
[button frame].size.width,
[button frame].size.height)];

[successImageView setFrame:CGRectMake(([[actionSheet superview] frame].size.width / 2) - [successImageView frame].size.width / 2,
  [successImageView frame].size.width,
  [successImageView frame].size.height)];

[actionSheet addSubview:successImageView];


End of UIActionSheet willPresentActionSheet example article.