I'm developing an iOS 5.0+ app with latest SDK.
I want to show a popup with some options to choose. But I don't know how to do it.
I have this code to show an UIActionSheet
with a seven buttons:
- (void)setUserActivity
{
activityActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:@""
destructiveButtonTitle:nil
otherButtonTitles:nil];
[activityActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
for (int index = 0; index < 7; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(activitySelected:)
forControlEvents:UIControlEventTouchDown];
CGRect frame = CGRectMake(0.0, index*40 + 10, 160.0, 40.0);
button.frame = frame;
button.tag = index;
[button setTitle:[NSString stringWithFormat:@"Button %d", index] forState:UIControlStateNormal];
[activityActionSheet addSubview:button];
}
CGRect toolbarFrame = CGRectMake(0, 0, activityActionSheet.bounds.size.width, 44);
UIToolbar* controlToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
[controlToolbar setBarStyle:UIBarStyleBlack];
[controlToolbar sizeToFit];
UIBarButtonItem* spacer =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem* setButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Set", nil)
style:UIBarButtonItemStyleDone
target:self
action:@selector(dismissActivityActionSheet)];
UIBarButtonItem* cancelButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil)
style:UIBarButtonItemStyleBordered
target:self
action:@selector(cancelActivityActionSheet)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil]
animated:NO];
[activityActionSheet addSubview:controlToolbar];
[activityActionSheet showInView:self.view];
[activityActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
And this is what I get:
How can I add a scroll to allow user click on button 6? Or, is there a better approach to do it?
You should add an UIScrollView between your buttons and the ActivityActionSheet...
...
UIScrollView *myButtonScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,160,441)];
for (int index = 0; index < 7; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(activitySelected:)
forControlEvents:UIControlEventTouchDown];
CGRect frame = CGRectMake(0.0, index*40 + 10, 160.0, 40.0);
button.frame = frame;
button.tag = index;
[button setTitle:[NSString stringWithFormat:@"Button %d", index] forState:UIControlStateNormal];
//[activityActionSheet addSubview:button];
[myButtonScroll addSubView:button];
}
[myButtonScroll setContentSize:CGSizeMake(160, 7*40 + 20)];
[activityActionSheet addSubView:myButtonScroll];
...
Assuming there are only 7 buttons on your view...