XIB的自定义Uiview不调整大小
我的目的是为MkannotationView创建自定义图像。
图像中的“服务项目”的数量在每个注释中都不同 - >应该可以动态地更改视图中的项目。
我的想法是创建自定义Uiview(XIB),以编程方式加载XIB并动态更改项目。然后,我将Uiview转换为uiimage。
//serviceannotationview.xib
//ServiceAnnotationView.class
class ServiceAnnotationView: UIView{
}
//扩展名Uiview->可以动态创建注释图像从代码加载XIB文件
extension UIView {
class func fromNib<T: UIView>() -> T {
return Bundle(for: T.self).loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
。
let view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
let serviceAnnotationView: ServiceAnnotationView = ServiceAnnotationView.fromNib()
view.image = serviceAnnotationView.asImage()
显示2个项目时的结果:
结果显示1个项目
https://i.sstatic.net/vxnrm.png“ alt =”在此处输入图像描述>
在这种情况下,Uiview应该自动收缩。 Uiview的宽度总是相同的。不管UistackView中存在多少个项目。
到目前为止,我尝试了什么? 我试图根据项目数来为Uiview指定框架。 - &gt;没有成功
,然后我根据项目数来更改Uiview的约束大小。 - &gt; 成功
由于UistackView,Uiview不得 。为什么在这种情况下没有发生?
My aim is to create custom image for MKAnnotationView.
The number of "service items" in image can be different for every annotation -> it should be possible to dynamically change items in view.
My idea was to create custom UIView (xib), load xib programmatically and change items dynamically. Then I would convert UIView to UIImage.
//ServiceAnnotationView.xib
//ServiceAnnotationView.class
class ServiceAnnotationView: UIView{
}
//extension UIView -> possible to load xib file from code
extension UIView {
class func fromNib<T: UIView>() -> T {
return Bundle(for: T.self).loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
Creating annotation image dynamically.
let view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
let serviceAnnotationView: ServiceAnnotationView = ServiceAnnotationView.fromNib()
view.image = serviceAnnotationView.asImage()
Result when showing 2 items:
Result when showing 1 items
In this case the UIView should automatically shrink.
The width of UIView is always the same. No matter how many items are present in UIStackView.
What I have tried so far?
I was trying to specify frame for UIView based on number of items. -> no success
Then I was changing constraint size of UIView based on number of items. -> no success
The UIView should automatically resize due to UIStackView. Why in this case is not happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经遗漏了有关设置以及实际使用此方法的大量信息,但这可能会有所帮助...
假设您有:
serviceanNotation View中的一些var/func
以显示/在堆栈视图中隐藏图像uiview
.asimage()
呼叫< Code> .setneedslayout()和
.layoutifneed()
nater nater calle.asimage()
:You've left out a lot of information about your setup and how you're actually trying to use this, but this might help...
Assuming you have:
ServiceAnnotationView
to show/hide the images in the stack viewUIView
extension for.asImage()
Call
.setNeedsLayout()
and.layoutIfNeeded()
before calling.asImage()
: