2

I have two questions with a long action sheet including 20 options (larger than the screen height):

Question 1. How to disable vertical bounces while scrolling?

Question 2. How to show the line separators while scrolling to the bottom?

Missing line separators of Question 2:

enter image description here

I am using Xcode 6.4 and iOS 8.4 simulator. The two issues also exist with my iPad running the same code on iOS 9.0.2.

I have created an action sheet using UIAlertControllerStyle.ActionSheet. I have added 20 options in the action sheet using addAction.

My ViewController.swift is as below:

override func viewDidLoad() {
    super.viewDidLoad()

    let button = UIButton(frame: CGRectMake(50, 60, 200, 20))
    button.setTitle("Press", forState: UIControlState.Normal)
    button.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    button.addTarget(self, action: "btnPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    view.addSubview(button)
}

func btnPressed(sender: UIButton) {
    let controller = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

    for item in 1...20 {
        controller.addAction(UIAlertAction(title: "Option \(item)", style: UIAlertActionStyle.Default, handler: { action in
        }))
    }
    controller.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

    presentViewController(controller, animated: true, completion: nil)
}

For Question 1:

Is there any bounces property that can be set like in UITableView: tableView.bounces = false to prevent long action sheets from vertical bounces while scrolling?

For Question 2:

This is different from "UIActionSheet is not showing separator on the last item on iOS 7 GM", which uses the deprecated UIActionSheet in stead of UIAlertController with UIAlertControllerStyle.ActionSheet. Adding the "Cancel" option to the action sheet does not seem to be a solution.

If I add a title or message to the action sheet or remove the "Cancel" option from it, bottom line separators are still missing.

Is it possible to show these missing lines?

Community
  • 1
  • 1
ehhhuang
  • 107
  • 1
  • 3
  • 9
  • Looks like a bug. Submit a bug report to Apple. Include a simple app that demonstrates the problem. Of course Apple won't be fixing any issues with iOS 8. It will be fixed (if not done already) in iOS 9.1.x or later. – rmaddy Oct 22 '15 at 14:00
  • OK. I've submitted a bug report to Apple. – ehhhuang Oct 25 '15 at 11:13

1 Answers1

0

If you'd like to disable scrolling in a view (and all of its subviews), this code should work just fine for you.

- (void)disableBouncingInView:(UIView *)view {
    for (UIView *subview in view.subviews) {
        if ([subview respondsToSelector:@selector(setBounces:)]) {
            UIScrollView *scrollView = (UIScrollView *)subview;
            [scrollView setBounces:NO];
        }

        [self disableBouncingInView];
    }
}

and call it like this:

[self disableBouncingInView:myAlertController.view];

EDIT:

I just saw that this question was tagged with swift, so here's that same idea in swift:

func disableBouncingInView(view : UIView) {
    for subview in view.subviews as [UIView]  {
        if let scrollView = subview as? UIScrollView {
            scrollView.bounces = false
        }

        disableBouncingInView(subview)
    }
}

and called like this:

        disableBouncingInView(myAlertController.view)
Fennelouski
  • 2,361
  • 1
  • 17
  • 25
  • 1
    Thank you for the solution to **Question 1**. When I use `disableBouncingInView(controller)` with Xcode 7.1, I got this: Cannot convert value of type 'UIAlertController' to expected argument type 'UIView'. So I modified the code as `disableBouncingInView(controller.view)` and then there is no more bouncing. – ehhhuang Oct 25 '15 at 11:11
  • Yep, sorry for that mistake. I'll fix it now! – Fennelouski Oct 25 '15 at 11:12
  • 1
    I just found Apple's guidelines in **UIAlertController Class Reference**: "The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified." [https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/) So I probably can't submit an app with this solution to App Store. – ehhhuang Oct 29 '15 at 15:11
  • Fortunately, this solution does not modify the view in any way, it only interacts with it. If you were adding a subview or modifying a cell in the tableview then there'd be a conflict, but interacting with (or disabling certain cosmetic interactions) shouldn't be an issue. – Fennelouski Oct 29 '15 at 15:14