XIB的自定义Uiview不调整大小

发布于 2025-01-28 03:40:59 字数 1758 浏览 2 评论 0原文

我的目的是为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.
enter image description here

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

enter image description here

//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:

enter image description here

Result when showing 1 items

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2025-02-04 03:40:59

您已经遗漏了有关设置以及实际使用此方法的大量信息,但这可能会有所帮助...

假设您有:

  • serviceanNotation View中的一些var/func以显示/在堆栈视图中隐藏图像
  • 您有一个工作uiview .asimage()
  • 已在图像(例如高度和宽度)

呼叫< Code> .setneedslayout()和.layoutifneed() nater nater calle .asimage()

    let serviceAnnotationView: ServiceAnnotationView = ServiceAnnotationView.fromNib()

    // however you're setting the number of images to show in the stack view        
    serviceAnnotationView.numItems = 3
    
    serviceAnnotationView.translatesAutoresizingMaskIntoConstraints = false
    
    serviceAnnotationView.setNeedsLayout()
    serviceAnnotationView.layoutIfNeeded()
    
    let img = serviceAnnotationView.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:

  • some var/func in your ServiceAnnotationView to show/hide the images in the stack view
  • you have a working UIView extension for .asImage()
  • you've set sizing constraints on the images (e.g. Height and Width)

Call .setNeedsLayout() and .layoutIfNeeded() before calling .asImage():

    let serviceAnnotationView: ServiceAnnotationView = ServiceAnnotationView.fromNib()

    // however you're setting the number of images to show in the stack view        
    serviceAnnotationView.numItems = 3
    
    serviceAnnotationView.translatesAutoresizingMaskIntoConstraints = false
    
    serviceAnnotationView.setNeedsLayout()
    serviceAnnotationView.layoutIfNeeded()
    
    let img = serviceAnnotationView.asImage()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文