Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController : UICollectionViewFlowLayout is not defined warning every time I try to bringup a UIAlertcontroller

I am using a UIAlertController for taking user input and updating a table cell. Everytime when I try to create a alert, I get this following warning in console

2015-11-19 17:51:42.034 SimpleTableView[5488:584215] the behavior of the UICollectionViewFlowLayout is not defined because:

2015-11-19 17:51:42.035 SimpleTableView[5488:584215] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

2015-11-19 17:51:42.036 SimpleTableView[5488:584215] The relevant UICollectionViewFlowLayout instance is <_UIAlertControllerCollectionViewFlowLayout: 0x7fd0a057c3d0>, and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <_UIAlertControllerCollectionViewFlowLayout: 0x7fd0a057c3d0>. 2015-11-19 17:51:42.036 SimpleTableView[5488:584215] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

The implementation is pretty straightforward as mentioned in numerous blogs,

func displayAlertInfo(){
    let alertController = UIAlertController(title: "New Race", message: "Type in a new race", preferredStyle: .Alert)
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in
        alertController.dismissViewControllerAnimated(true, completion: nil)
    }
    let addAction = UIAlertAction(title: "Add", style: .Default) { (_) in
        let textFieldInput = alertController.textFields![0] as UITextField
        let newRace = textFieldInput.text?.capitalizedString
        DataManager.sharedInstance.addRace(species: self.species, race: newRace!)
        let newIndexPath = NSIndexPath(forRow: self.races.count - 1, inSection: 0)
        self.racesTableVew.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.placeholder = "New Race"
    }

    alertController.addAction(cancelAction)
    alertController.addAction(addAction)

    self.presentViewController(alertController, animated: true, completion: nil)
}

I call this function on a navigation button tap. The alert displays correctly, but I keep on getting this warning everytime I try to tap the button which creates the alert. What am I doing wrong or am I skipping something ?

EDIT : Added Screenshot : The alert is called from navigation item button, over a tableView.

enter image description here

like image 753
Shane D Avatar asked Nov 19 '15 12:11

Shane D


People also ask

Can the uialertcontroller class be subclassed?

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.

How do I configure alerts and action sheets in UIKit?

Use this class to configure alerts and action sheets with the message that you want to display and the actions from which to choose. After configuring the alert controller with the actions and style you want, present it using the present (_:animated:completion:) method. UIKit displays alerts and action sheets modally over your app's content.

Why does the alert controller have a reference to each field?

The alert controller maintains a reference to each text field so that you can access its value later. 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.

How do I add text fields to the alert interface?

When configuring an alert with the UIAlertController.Style.alert style, you can also add text fields to the alert interface. The alert controller lets you provide a block for configuring your text fields prior to display. The alert controller maintains a reference to each text field so that you can access its value later.


3 Answers

You need to call

alertcontroller.view.setNeedsLayout()

right before

self.presentViewController(alertController, animated: true, completion: nil)

Source: https://forums.developer.apple.com/thread/18294 (last answer)

like image 189
Mak Avatar answered Nov 08 '22 12:11

Mak


I had a similar problem: worked fine on iPhone 6 Plus portrait, failed in landscape. I made one small change: from

 message: nil,  

to:

 message: "this is a nice long line of crapthis is a nice long line of crapthis is a nice long line of crap",

..which was long enough to wrap, adding two newlines. And my problem was solved. But a shorter message that didn't line-wrap at least twice still caused the error to occur.

I then found that

 message: "\n\n"  

worked just as well, but a message of a single newline failed. And if I set title: nil, I needed more newlines in message. It appears to be related to the total height of the alert, title and message combined.

Note: other alerts without a text field didn't show the same problem. And in my case, setNeedsLayout() didn't make any difference.

(Of course, adding that many lines makes the alert too tall to appear on smaller iPhones in landscape once you include the pop-up keyboard, generating the same flood of error messages. So I wound up making the addition of those lines conditional on the height and width of the screen being larger than a set value.)

like image 37
Adam Wilt Avatar answered Nov 08 '22 10:11

Adam Wilt


I also had same issue in my screen for only iphon6 plus screen. Please check, if you are not passing empty title, if title is empty , pass it nil like :

 UIAlertController *confirmAlert = [UIAlertController alertControllerWithTitle:nil message:displayMessage preferredStyle:UIAlertControllerStyleAlert];

instead of

  UIAlertController *confirmAlert = [UIAlertController alertControllerWithTitle:@"" message:displayMessage preferredStyle:UIAlertControllerStyleAlert];

it fixed my problem. Good luck..

like image 36
Ash Avatar answered Nov 08 '22 11:11

Ash