UIStackView subclasses written in Swift may crash due to unimplemented init(frame:)

Originator:clafou
Number:rdar://25645113 Date Originated:2016-4-10
Status:Open Resolved:
Product:iOS Product Version:9.3.1
Classification:Crash Reproducible:Always
 
Summary:
-[UIStackView initWithArrangedSubviews:] has an undocumented dependency on its subclass's init(frame: CGRect), which Swift does not require implementing.

Steps to Reproduce:
Write the following code:

class SubclassedStackView: UIStackView {

    init(text: String, subtext: String) {
        let textlabel = UILabel()
        let subtextLabel = UILabel()
        textlabel.text = text
        subtextLabel.text = subtext
        super.init(arrangedSubviews: [textlabel, subtextLabel])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

If you then use it such as this:

let stackView = SubclassedStackView(text: "Test", subtext: "Uh-oh!")

You get a runtime exception with the following message:

fatal error: use of unimplemented initializer 'init(frame:)' for class 'test.SubclassedStackView'

A look at the call stack shows that the base initializer -[UIStackView initWithArrangedSubviews:] is attempting to call init(frame: CGRect) on the subclass, which is intentionally left unimplemented.

Expected Results:
UIStackView should not call int:frame on its subclass when called by this very subclass's other initialiser.

Actual Results:
fatal error: use of unimplemented initializer 'init(frame:)' for class 'test.SubclassedStackView'

Version:
iOS 9.3.1

Notes:


Configuration:
Any iOS device

Attachments:
'print  10-04-2016, 14-26.crash' was successfully uploaded.

Comments

Believed fixed in iOS 10

Apple Developer Relations 05-Jul-2016 07:38 PM

We believe this issue is resolved in the latest iOS 10 beta.

Please test with the latest iOS beta. If you still have issues, please update your bug report with any relevant logs or information that could help us investigate.

https://developer.apple.com/download/


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!