简体   繁体   English

如果启用了自动布局,则不会调用drawRect

[英]drawRect not being called if auto layout enabled

XCode 5, Mac OS 10.9.4, iOS 7. XCode 5,Mac OS 10.9.4,iOS 7。

I'm working on a project that uses a lot of view “cells” that go into Scroll and Table Views. 我正在开发一个项目,该项目使用了很多视图“单元”,这些“单元”已进入“滚动视图”和“表视图”。 Each “cell” View is in separate .xib file and is loaded something like this: 每个“单元”视图都位于单独的.xib文件中,并加载如下所示的内容:

+(instancetype) newViewOfKind:(NSString*) theViewKind withItemInfo:(LTWItemInfo*) theItemInfo
{
NSString*   myClassName;
LTWItemView*    myNewCell;

myClassName = NSStringFromClass([self class]);
myNewCell = [[super class] newViewOfKind:theViewKind withItemInfo:theItemInfo andNIBBaseName:myClassName];

if (myNewCell.pItemActivityIndicator != nil)
    myNewCell.pItemActivityIndicator.hidesWhenStopped = YES;

myNewCell.pItemDownloadImageLoadedFlag = NO;
myNewCell.pItemDownloadImage = nil;
[myNewCell setItemInfo:theItemInfo];

return myNewCell;
}

This works well and is being used in lots of places in the App. 这很好用,并且已在App中的许多地方使用。

This project was originated before Auto-Layout was around and it is off in all the cell .xib files. 该项目起源于自动布局,并且在所有单元.xib文件中均处于关闭状态。

The .xib file for each of these views has a “standard” layout that looks like this: 每个视图的.xib文件都有一个“标准”布局,如​​下所示:

LTWItemView.xib — Auto-Layout off. LTWItemView.xib —关闭自动布局。

LTWItemView - The View Class Itself. LTWItemView-视图类本身。

Subview of LTWItemView: “FrameRect” - View of Class “LTWDrawFrameView” (this is a normal view, but drawRect overridden to draw a frame around the view). LTWItemView的子视图:“ FrameRect”-类“ LTWDrawFrameView”的视图(这是常规视图,但是drawRect被覆盖以在视图周围绘制框架)。

Subviews of “FrameRect” view, other views UILabels, UIImageView's, UIButton's, etc. “ FrameRect”视图的子视图,其他视图UILabel,UIImageView的,UIButton的等等。

The problem is that the drawRect method in “FrameRect” does not get called and its background colour doesn't get set either. 问题是“ FrameRect”中的drawRect方法没有被调用,其背景色也没有被设置。 When I first created this view, I had Auto-Layout enabled by mistake and in this case the background colour is set ok and drawRect is called. 第一次创建此视图时,我错误地启用了自动布局,在这种情况下,背景色设置为ok,并调用了drawRect。 I've verified this. 我已经证实了这一点。

The problem is that if I enable Auto-Layout, it causes problems elsewhere and it's too big a job to convert the project to use it. 问题是,如果启用“自动布局”,则会在其他地方引起问题,并且转换项目以使用它的工作量太大。

Also, inside some of “FrameRect” views, there are other, smaller LTWDrawFrameView's and these draw their frame and set their background colours ok. 另外,在某些“ FrameRect”视图中,还有其他较小的LTWDrawFrameView,它们绘制其框架并设置其背景色正常。 It seems like it just doesn't work with the first subview inside the view loaded from the nib. 看来这与从笔尖加载的视图中的第一个子视图不起作用。

Any ideas on how to solve this would be greatly appreciated. 任何有关如何解决此问题的想法将不胜感激。


I've worked around the problem by adding a "dummy" view as the first subview of the Cell View in the .xib file and putting the "FrameRect" (and all its subviews) inside this view. 我通过在.xib文件中将“虚拟”视图添加为Cell视图的第一个子视图并将“ FrameRect”(及其所有子视图)放入该视图中来解决该问题。 This seems to work fine and solves my immediate problem, but I'd love to know if there is a better fix for this. 这似乎可以很好地解决我的当前问题,但是我很想知道是否对此有更好的解决方案。

我没有完全解决您的问题,但是通常需要调用其drawRectFunction的视图应该将其contentMode设置为UIViewContentModeRedraw

yourView.contentMode = UIViewContentModeRedraw;

Disambigous layout can cause that. 歧义的布局可能会导致这种情况。 For example, you specified NSLayoutConstraint's about the vertical placement of the view, but didn't say anything about horizontal. 例如,您为视图的垂直位置指定了NSLayoutConstraint,但没有指定水平。 The situation can be treacherous as UIKit tries it's best and places the view at least somewhere, but drawRect: is not called. 当UIKit尝试最佳状态并将视图至少放置在某个位置时,情况可能很危险,但是未调用drawRect:。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM