iOS(swift)
[Swift 4] Fakebook 만들기 - 3. AutoLayout, Dynamic(AutoSize) CollectionView Cell, Rounded Image
광그로
2017. 11. 25. 03:28
프로젝트 코드 : https://github.com/LeeGwangYong/iOS_swift/tree/master/FaceBook
참고 사이트 : https://www.youtube.com/watch?v=NJxb7EKXF3U&list=PL0dzCUj1L5JHDWIO3x4wePhD8G4d1Fa6N
1. CustomCollectionViewCell 생성하기
2. Dynamic(AutoSize) CollectionView Cell AutoLayout
ㄱ. Cell 내부에 새로운 UIView를 넣는다.
ㄴ. UIView의 Cosntraint를 Top, Bottom, Leading, Trailling, Width를 지정
ㄷ. Width의 @IBOutlet을 CustomCollectionViewCell.swift에 생성
ㅁ. CustomCollectionViewCell.swift 수정
12345678910111213141516 override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func awakeFromNib() { setupCell() } func setupCell(){ self.contentView.translatesAutoresizingMaskIntoConstraints = false widthConstraint.constant = UIScreen.main.bounds.size.width } cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func awakeFromNib() { setupCell() } func setupCell(){ self.contentView.translatesAutoresizingMaskIntoConstraints = false widthConstraint.constant = UIScreen.main.bounds.size.width } | cs |
3. Rounded Image
@IBDesignable 키워드가 추가되어 있어, 스토리보드에서도 잘 나온다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import UIKit @IBDesignable class RoundedImageView: UIImageView { override init(image: UIImage?) { super.init(image: image) } override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } @IBInspectable var divisor: CGFloat = 2.0 { didSet{ self.layer.cornerRadius = self.frame.width/divisor } } @IBInspectable var borderColor:UIColor = UIColor.white { didSet{ self.layer.borderColor = borderColor.cgColor } } @IBInspectable var borderWidth: CGFloat = 2.0 { didSet{ self.layer.borderWidth = borderWidth } } override func layoutSubviews() { self.layer.borderWidth = borderWidth self.layer.masksToBounds = false self.layer.borderColor = borderColor.cgColor self.isUserInteractionEnabled = true self.layer.cornerRadius = self.frame.width/divisor self.contentMode = .scaleAspectFit self.clipsToBounds = true } } | cs |