UIImage方面填充和clipsToBounds

时间:2019-03-10 12:12:06

标签: swift uiimage cliptobounds

我想将图像设置为UIImage,但是一旦设置它,UIImageView就会变大并越过其他元素。

这里提供了一个解决方案: Crop UIImage to fit a frame image

但是我无法使其正常工作,我像提供的解决方案一样设置属性。

也许有人可以帮我一下:

不设置图像:

enter image description here

设置图像后: enter image description here

class Cell: UICollectionViewCell, CollectionViewCellConfigurable {

    var image = UIImageView()
    var dateDay = UILabel()
    var dateMonth = UILabel()
    var title = UILabel()

    lazy private var dateContainer: UIView = {
       let v = UIView()
        v.sv(dateDay, dateMonth)

        dateDay.heightAnchor.constraint(equalTo: self.dateMonth.heightAnchor, multiplier: 1).isActive = true
        dateDay.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 5).isActive = true
        dateDay.trailingAnchor.constraint(equalTo: v.trailingAnchor, constant: -5).isActive = true
        dateDay.topAnchor.constraint(equalTo: v.topAnchor, constant: 15).isActive = true
        dateDay.bottomAnchor.constraint(equalTo: dateMonth.topAnchor, constant: -5).isActive = true

        dateMonth.heightAnchor.constraint(equalTo: self.dateDay.heightAnchor, multiplier: 1).isActive = true
        dateMonth.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 5).isActive = true
        dateMonth.trailingAnchor.constraint(equalTo: v.trailingAnchor, constant: -5).isActive = true
        dateMonth.topAnchor.constraint(equalTo: dateDay.bottomAnchor, constant: 5).isActive = true
//        dateMonth.bottomAnchor.constraint(greaterThanOrEqualTo: v.bottomAnchor, constant: -5).isActive = true
        return v
    }()

    lazy private var container: UIView = {
        return UIView()
    }()

    func configureCellAtIndexPath(item: Journaling) {
        self.image.image = UIImage.baliBeach
        self.image.backgroundColor = .green
        self.dateDay.text = "16"
        self.dateMonth.text = "May"
        self.title.text = "Text visible"

    }

    override init(frame: CGRect) {
        super.init(frame:frame)
        setUpLayout()
        additionalSetUp()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func additionalSetUp() {
        backgroundColor = .clear
        let colorText: UIColor = .grau

        dateDay.textColor = colorText
        dateMonth.textColor = colorText
        title.textColor = colorText
        dateDay.textAlignment = .center
        dateMonth.textAlignment = .center
        title.textAlignment = .center
        image.contentMode = .scaleAspectFill
        image.clipsToBounds = true
        image.autoresizesSubviews = true

    }

    func setUpLayout() {
        sv(dateContainer, container)

        container.sv(image,title)

        dateContainer.widthAnchor.constraint(equalToConstant: 45).isActive = true
//        dateContainer.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.15).isActive = true
        dateContainer.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
        dateContainer.trailingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
        dateContainer.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
        dateContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true

//        container.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true
        container.leadingAnchor.constraint(equalTo: dateContainer.trailingAnchor, constant: 0).isActive = true
        container.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
        container.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
        container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true

        image.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 5).isActive = true
        image.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -5).isActive = true
        image.topAnchor.constraint(equalTo: self.topAnchor, constant: 15).isActive = true
        image.bottomAnchor.constraint(equalTo: title.topAnchor, constant: -5).isActive = true

        title.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
        title.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: 0).isActive = true
        title.topAnchor.constraint(equalTo: image.bottomAnchor, constant: 5).isActive = true
        title.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: 0).isActive = true
    }
}

1 个答案:

答案 0 :(得分:2)

您需要将标题标签的垂直压缩优先级提高,因为imageView等于750,所以它占用了空间

\DB::beginTransaction();

try{
    $user->save();
    //role
    $user->attachRole($role);
    //send email

    Mail::to($user->email)->send(new ActivationCode($user));
    \DB::commit();
    return response()->json([
        'status' => 'success',
        'message' => 'Berhasil mendaftar, cek email anda untuk mendapatkan kode aktivasi'
    ], 201);    

}catch(\Exception $e){
    \DB::rollback();
    return response()->json([
        'status' => 'fail',
        'message' => 'Terjadi kesalahan, harap periksa kembali form pendaftaran'
    ], 400);  
}