I just found out one more reason in my case:
When I used "Editor -> Debug Selected Views", I saw that it crashed because I used CoreGraphics to create the image and used it as the background image for the button:
class func imageWithColor(color: UIColor, size: CGSize) -> UIImage { let rect = CGRectMake(0.0, 0.0, size.width, size.height); UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0); let context = UIGraphicsGetCurrentContext();
The reason is because the size is (0.0, 0.0). Why is this? I'm calling
super.setBackgroundImage(UIImage.imageWithColor(bgColor, size: self.bounds.size), forState: .Normal)
But in IB self.bounds.size is still 0 at this point! So I changed one line:
let rect = CGRectMake(0.0, 0.0, max(size.width, 1.0), max(size.height, 1.0));
And now it works :)
Apple should provide a list with Dos and Don'ts regarding IB ...
benjamin.ludwig Apr 21 '16 at 5:28 2016-04-21 05:28
source share