自定义UIView;当视图的大小低于特定垃圾箱时执行操作

问题描述

我有一个自定义视图,可以将其动态添加到滚动视图内的另一个(图像)视图。因此,图像大小将基于scrollView的(初始)缩放比例而变化。 所以,基本上,我想在将此视图添加到(image)View时设置一些属性和约束。我以为可以在didMovetoSuperView()上处理,但是帧和边界始终为0?

如何解决这个问题?

我的自定义视图:

//MARK: - Properties
    private var annotation: Annotation!
    
    private var pinImageView = UIImageView()
    private var completedImageView = UIImageView()
    private var arrowImageView = UIImageView()
    private var pinTitleLabel = ProjectTitleLabel(withTextAlignment: .center,andFont: UIFont.preferredFont(forTextStyle: .callout),andColor: .label)
    
    //MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required convenience init(with annotation: Annotation) {
        self.init(frame: .zero)
        self.annotation = annotation
        configure()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: - Configure view
    private func configure() {
        (...)
    }
    
    override func didMovetoSuperview() {
        print("HERE: \(self.frame.height)",bounds.height,intrinsicContentSize)
        //Compensate for when the pin view becomes really small (on very low res images)
        if self.bounds.height <= 40 {
            pinTitleLabel.minimumScaleFactor = 0.45
            pinTitleLabel.font = UIFont.preferredFont(forTextStyle: .caption1)
            pinTitleLabel.heightAnchor.constraint(equalToConstant: 12).isActive = true
            print("SMALL")
        } else {
            pinTitleLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true
            print("norMAL")
        }
    }
}

以及添加此视图的代码

    var annotation: Annotation? {
        didSet {
            //remove any prevIoUs pin
            mapImageLocationPin?.removeFromSuperview()

            if let location = annotation?.location {
                //Activate the corresponding Map Image and update picker
                selectedMapImage = location.map
                mapImagePickerView.selectRow(mapImagesController.getRowFor(location.map),inComponent: 0,animated: true)

                //Instantiate new Pin with annotation
                mapImageLocationPin = MapImageLocationPin(with: annotation!)
                mapImageView.addSubview(mapImageLocationPin!)

                //Desired height and width
                let desiredWidth: CGFloat = 160
                let desiredHeight: CGFloat = 80
                
                //compensate for scrollView zoomscale,but never bigger than desired width & height!
                let width = min(desiredWidth,desiredWidth / mapImageScrollView.minimumZoomScale)
                let height = min(desiredHeight,desiredHeight / mapImageScrollView.minimumZoomScale)
                print("w: \(width) x h: \(height)")
                
                //center the pin image horizontal on location
                let y = location.coordinateY
                let x = location.coordinateX - (width / 2)
                
                //position (frame) the pin
                mapImageLocationPin?.frame = CGRect(x: x,y: y,width: width,height: height)
            }
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)