0

Делаю кастомный header для tableview. в методе сета фотографии делаю cornerradius и clipto bounds, но фото остается квадратным.

final class InfoHeaderView: UITableViewHeaderFooterView {
    static let reuseIdentifier: String = String(describing: self)
    @IBOutlet var imageView: UIImageView! //= UIImageView()
    @IBOutlet var nickname: UILabel! //= UILabel()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        imageView = UIImageView()
        //imageView.layer.cornerRadius = imageView.frame.width / 2
        //imageView.clipsToBounds = true
        nickname = UILabel()
        contentView.backgroundColor = .systemBlue
        contentView.addSubview(imageView)
        contentView.addSubview(nickname)

        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.widthAnchor.constraint(equalToConstant: 125.0).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: 125.0).isActive = true
        imageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
        imageView.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true

        nickname.translatesAutoresizingMaskIntoConstraints = false
        nickname.topAnchor.constraint(equalTo: imageView.safeAreaLayoutGuide.bottomAnchor, constant: 5).isActive = true
        nickname.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
        nickname.heightAnchor.constraint(equalToConstant: 15).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func setImage(with img: UIImage) {
        imageView.image = img
        imageView.layer.cornerRadius = imageView.frame.width / 2
        imageView.clipsToBounds = true
    }

    func setNickname(with nickname: String) {
        self.nickname.text = nickname
    }
}
2
  • Выложите код в виде текста
    – schmidt9
    16 апр 2020 в 19:08
  • @schmidt9 updated 16 апр 2020 в 19:38

1 ответ 1

0

Используйте вместо imageView.clipsToBounds = true imageView.layer.masksToBounds = true

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.