如何解决自定义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 举报,一经查实,本站将立刻删除。