The situation in which subviews were not being redrawn seems to have been a xib
problem. After spending some time getting nowhere I decided to recreate the xib
by taking a different xib (which used the same custom view) and cutting it
down. This worked without any problems. Then I compared the two xibs in
TextWrangler and found that the one which didn’t work had a view where
“wantsLayer” was set to YES - and this corresponded to a “Core Animation Layer”
checkbox in Interface Builder.
I didn’t intentionally select this checkbox, and I didn’t originally look at
this pane (View Effects) when I was trying to spot differences between the view
that worked and the view that didn’t. But in my first attempt at creating this
xib (where the problem occurred) I copied and pasted a view from another xib,
whereas in my second attempt I duplicated the entire xib and modified it - so
maybe the checkbox property was set as a side effect of copying and pasting.
One other strange behaviour with the problem xib occurred when I added some
debugging code to try and find out why the subview wasn’t being redrawn:
view.needsDisplay = true
print("view.needsDisplay: \(view.needsDisplay)”)
and this consistently printed “ view.needsDisplay: false” after
view.needsDisplay had been set to true. Despite this, “draw” (= drawRect in
ObjC) was being called for “view” - but not for view’s subviews.
My conclusion (for now) is that “needsDisplay” causes subviews to be redrawn -
except when “wantsLayer” has been set to true.
Does that make sense?
Jeremy
--
> On 8 Mar 2017, at 17:12, Jeremy Hughes <[email protected]> wrote:
>
> Thanks - that seems reasonably clear, and it agrees with what I previously
> thought, but I’ve got a situation where it doesn’t seem to be happening.
>
> There are contradictory opinions on Stack Overflow:
>
> http://stackoverflow.com/questions/8718290/setneedsdisplay-and-subviews
> http://stackoverflow.com/questions/11480341/setneedsdisplay-does-not-trigger-drawrect-in-subviews-as-expected
>
> Jeremy
>
> --
>
>> On 8 Mar 2017, at 17:01, Ben Kennedy <[email protected]> wrote:
>>
>>
>>> On 08 Mar 2017, at 8:46 am, Jeremy Hughes <[email protected]>
>>> wrote:
>>>
>>> If needsDisplay is set to true for an NSView, does that also cause subviews
>>> to be redrawn?
>>
>> Admittedly I've been mostly doing iOS development for the last several years
>> and barely any Mac lately, but, according to the current docs:
>>
>> needsDisplay: "The displayIfNeeded methods check the value of this property
>> to avoid unnecessary drawing, and all display methods set the value back to
>> false when the view is up to date."
>>
>> display(): "Displays the view and all its subviews if possible, invoking
>> each of the NSView methods lockFocus(), draw(_:), and unlockFocus() as
>> necessary."
>>
>> Thus, it seems to follow that so long a custom view's display() calls super,
>> then all of its subviews should also be drawn when its needsDisplay is true.
>>
>> What sort of contradictions are out there?
>>
>> b
>>
>
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]