Ríomhaire Ríomhaire - 8 months ago 84
iOS Question

Set contentMode of UIImageView

In Obj-C

imageView.contentMode = UIViewContentModeScaleAspectFill;


would set the contentMode.

Why does

imageView.contentMode = UIViewContentModeScaleAspectFill


not work in Swift?

Answer

Somewhat confusingly, Swift drops the prefix for ObjC enum values:

imageView.contentMode = .ScaleAspectFill

This is because Swift already knows what enum type is being used. Alternatively, you can specify the enum too:

imageView.contentMode = UIViewContentMode.ScaleAspectFill