@IBDesignable-视图未按预期呈现 [英] @IBDesignable - view not rendering as expected

查看:54
本文介绍了@IBDesignable-视图未按预期呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 UIView 子类,我正在尝试使用 @IBDesignable 呈现该子类.

我有一个名为 image @IBInspectable 属性,该属性是在界面构建器中设置的.

当我打开界面生成器时,我选择的 image 在视图中无法正确呈现,也就是说,自动布局看起来有些麻烦.当我检查 Report导航器时,我在 Interface Builder 组中收到一条警告.

 显示所有消息(空):-pie被忽略.仅在链接主可执行文件时使用 

这发生在链接< IBDESIGNABLE_EXECUTABLE> 部分下.

我不知道该如何解决.从 SO 尝试了许多解决方案.清理构建文件夹,清除派生数据等.

请提供您的宝贵意见或解决此问题的方法.谢谢!:)

感谢@Rob,我可以将问题归零.

我所拥有的:

具有属性的 IBDesignable UIView 子类:

  • 名为 image @IBInspectable 属性.
  • ImageView
  • 一个ScrollView

我使用

初始化并添加ScrollView和ImageView作为子视图.

 是否需要公共init?(编码器aDecoder:NSCoder){super.init(编码器:aDecoder)initialiseImageView()}覆盖init(frame:CGRect){super.init(frame:框架)initialiseImageView()} 

问题:

initialiseImageView()中:
我将 scrollView 添加为 UIView 的子视图,并添加 ImageView 作为 scrollView 的子视图,以使用 viewForZooming 委托.我还添加了约束以使视图保持在适当位置.
问题是当我使用时:

  self.translatesAutoresizingMaskIntoConstraints = false 

如果使用此命令,则会在运行时收到自动布局警告.但是,尽管图像正确呈现,但 imageView interface builder 中却放错了位置.

如果我发表相同的意见,那么一切都与 interface builder 中的预期一样.但是由于 AutoresizingMaskIntoConstraints ,UI在运行时中断.


我在 github 中有代码.我正在尝试 TN2154

中提到的后一种方法

我将所有 AutoresizingMaskIntoConstraints 设置为false,并在代码本身中为子视图添加约束. UIView 子类的约束直接添加到了界面构建器中,并且到目前为止没有警告存在.该代码也可以在运行时按预期工作. IBDesignable 部分无法正常工作.


奇怪的是,当我删除行

  self.translatesAutoresizingMaskIntoConstraints = false 

一切正常.我以前在运行时遇到的错误现在消失了.我不知道我是否做了其他修复.非常感谢您帮助指出问题@Rob.

解决方案

顺便说一句,我只是对 @IBInspectable 做了一个快速的 @IBDesignable ,它是一个UIImage 和渲染该图像的 draw(rect:)可以正常工作:

  @IBDesignable类CustomView:UIView {@IBInspectable var图像:UIImage?覆盖func draw(_ rect:CGRect){图片?.draw(at:CGPoint.zero)}} 

当我在一个单独的目标中执行此操作时(就像对可设计对象一样),我还会看到相同的警告:

ld:警告:-pie被忽略.仅在链接主要可执行文件时使用

我怀疑警告可能是鲱鱼.的 man 页描述了 -pie 选项,如下所示:

这是一种特殊的主要独立于位置(PIE)的主要可执行文件.

如果转到可设计目标的链接设置,然后在链接"设置中更改生成位置相关的可执行文件",则可以切换此设置.然后,该警告将消失.就我个人而言,我从未注意到该警告,也从未更改过此设置,也从未注意到任何不利影响,但是如果您出于自己的满意程度来确认此设置,请尝试更改此设置,看看是否可以得到警告离开.

最重要的是,您的 @IBDesignable 问题可能存在于其他地方,但是很难根据所提供的信息进行诊断.我们需要更多信息(或者最好是 MCVE ).


很难在不注意警告的情况下评论自动布局警告,但是我猜(a)您正在添加子视图;但是(b)不设置该子视图的约束;因此(c)当您设置 translatesAutoresizingMaskIntoConstraints = false 时,您的约束是模棱两可的,因此会在运行时导致错误消息和意外的布局.也许这是滚动视图特质的症状.但是,如果不看到(a)实际的自动布局错误,很难说.(b)您添加了 @IBDesignable 的子视图;(c)您为子视图添加了哪些约束条件.

最底线,请确保以编程方式添加的子视图明确定义了所有约束.还要记住,滚动视图子视图的约束与许多约束的行为不同,它们定义了滚动视图的contentSize而不是子视图的大小.(关于后一点,请参见 TN2154 .)

I have a custom UIView subclass that I am trying to render using @IBDesignable.

I have an@IBInspectable property named image, which I set from the interface builder.

When I open the interface builder the image that I choose is not rendered properly in the view, that is, it looks like some trouble with autolayout. When I checked the Report navigator I am getting a warning in the Interface Builder group.

Showing All Messages
(null): -pie being ignored. It is only used when linking a main executable

This happens under the Link <IBDESIGNABLE_EXECUTABLE> section.

I have no idea how to fix this. Tried many solutions from SO. Cleaning build folder, clearing derived data etc. Nothing worked.

Please provide your valuable comments or a solution to this problem. Thanks! :)

EDIT:

Thanks to @Rob I could zero in to the problem.

What I have:

An IBDesignable UIView Subclass with properties:

  • an@IBInspectable property named image.
  • an ImageView
  • a ScrollView

I initialise and add the ScrollView and ImageViews as subviews using

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initialiseImageView()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    initialiseImageView()
}

Problem:

In initialiseImageView():
I add the scrollView as subview of the UIView and ImageView as the subview of the scrollView to use the viewForZooming delegate. I also add constraints to keep the views in place.
The problem is when I use:

self.translatesAutoresizingMaskIntoConstraints = false

If I use this I get autolayout warnings at runtime. But the imageView is misplaced in interface builder, although the image is rendered correctly.

If I comment the same, everything is as expected in interface builder. But the UI breaks at runtime due to AutoresizingMaskIntoConstraints.


I have the code in github. I am trying the latter approach mentioned in TN2154

I am setting all AutoresizingMaskIntoConstraints to be false and adding the constraints for the subviews in the code itself. The constraints for the UIView subclass are added in the interface builder directly and no warnings exist as of now. The code works as expected at runtime also. The IBDesignable part is the one that is not working as expected.


Strangely enough, When I removed the line

self.translatesAutoresizingMaskIntoConstraints = false

everything is working perfectly. The error that I used to get at runtime is now gone. I don't know if something else that I did fixed it. Thanks a lot for helping to pin point the issue @Rob.

解决方案

BTW, I just did a quick @IBDesignable with an @IBInspectable that was a UIImage and a draw(rect:) that rendered that image and it worked fine:

@IBDesignable class CustomView: UIView {

    @IBInspectable var image: UIImage?

    override func draw(_ rect: CGRect) {
        image?.draw(at: CGPoint.zero)
    }

}

When I do that in a separate target (as one should with designables), I also see the same warning:

ld: warning: -pie being ignored. It is only used when linking a main executable

I suspect that warning may be a red herring. The man pages for ld described -pie option as follows:

This makes a special kind of main executable that is position independent (PIE).

You can toggle this setting if you go to the link settings for your designables' target and change the "Generate Position-Dependent Executable" in the "Linking" settings. Then that warning will go away. Personally, I've never noticed that warning and never changed this settings, and never noticed any adverse affects, but if you want to confirm this for your own sense of satisfaction, try changing this setting and see if you can get the warning to go away.

Bottom line, your @IBDesignable problem probably rests elsewhere, but it's hard to diagnose on the basis of the information provided. We need more information (or, ideally, a MCVE).


It's hard to comment on your autolayout warnings without seeing what those warning are, but I'd guess that (a) you're adding a subview; but (b) not setting the constraints for said subview; and therefore (c) that when you set translatesAutoresizingMaskIntoConstraints = false, that your constraints are ambiguous and therefore result in error messages and unexpected layout at runtime. Or maybe it's a symptom of the idiosyncrasies of scroll views. But it's hard to say without seeing (a) actual autolayout error; (b) what subviews your @IBDesignable added; and (c) what constraints you added for your subviews.

Bottom line, make sure that your programmatically added subviews have all of their constraints unambiguously defined. Also remember that constraints for scrollview subviews act differently that many constraints, defining the contentSize of the scroll view rather than the size of the subviews. (On this latter point, see TN2154.)

这篇关于@IBDesignable-视图未按预期呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆