[swift] imageViewの画像の表示方法まとめ
imageViewの画像の表示方法まとめ
imageViewの画像の表示モード(縦横比固定など)はcontentModeで指定できる
//UIImageViewの生成 let imageView = UIImageView(image: UIImage(named: "image.png")) //UIImageViewのサイズを自動的にimageのサイズに合わせる imageView.contentMode = UIViewContentMode.Center self.view.addSubview(imageView)
UIViewContentModeの選択肢と効果は以下の通り
enum {
UIViewContentModeScaleToFill, // これがデフォルト。UIImageViewにめいっぱいひろげる
UIViewContentModeScaleAspectFit, // 画像のaspect比を維持し、ちょうどはいるようにする
UIViewContentModeScaleAspectFill, // 画像のaspect比を維持し、めいっぱい広げる(はみ出した分がみれなくなる)
UIViewContentModeRedraw, // UIViewContentModeScaleToFill これと同じに見えるけどなんだろう・・
UIViewContentModeCenter, // 画像サイズをそのままに、真ん中を表示
UIViewContentModeTop, // 上を
UIViewContentModeBottom, // 下を
UIViewContentModeLeft, // 左を
UIViewContentModeRight, // 右を
UIViewContentModeTopLeft, // 左上を
UIViewContentModeTopRight, // 右上を
UIViewContentModeBottomLeft, // 左下を
UIViewContentModeBottomRight, // 右下を
}