Swift equivalent of the Xojo "DrawInto" function

This function (for macOS) will take any NSControl that is based on NSView (which is just about all of them) and return an NSImage snapshot of the control

func takeSnapShot(_ tempCtrl : NSView)  -> NSImage {
   private var osCtrlImage    :  NSImage?  = nil
   if let rep = tempCtrl.bitmapImageRepForCachingDisplay(in: tempCtrl.bounds) {
      tempCtrl.cacheDisplay(in:    tempCtrl.bounds, to: rep)
      osCtrlImage = NSImage(size:  tempCtrl.bounds.size)
   return osCtrlImage

Yeah, it’s pretty cool that function. I find really useful for animating the changing of a preferences pane, so you grab the current view, throw it into a new view, line 'em both up and use Core Animation to transition between the old state and the new.

1 Like

Note : this routine for some reason has issue with a TabPanel control. it draws everything EXCEPT the Segmented Control “Tabs”

huh… I’ve not tried it with a tabpanel control, but I have used it with a segmented control and that did work. Weird.

yes… seg ctrl works, as do most others I’ve tried (about 25 so far)

but this is what a tabpanel produces… notice the body of the control is there, as is the outline of where the “tabs” should go, but not the tabs themselves

Yeah, it is odd. Have you checked the view hierarchy to make sure that the segmented control is nested inside the panel view.

not qute sure how to do that, and even so, how could I “fix” it.

I have also notices that sometimes even Xojo does this, but there it appears when I resize the control… but not here.

I don’t know how to do it in Xcode, how I’ve done it via Xojo is at runtime and list out all the attached sub NSViews.

Its like the NSTableView and NSTextView are wrapped in NSScrollViews, but we kinda ignore those. So I wondered if that might be the case with the Tab Panel.

In recent years, it’s gotten pretty flaky with displaying controls, I often end up with a window with controls no longer visible in the IDE. I just assumed it was another bug that lost out in favor or “2.0 All The Things™”.

I actually noticed it the other day in Xojo 2019r1.1 to see what happened if I duplicated the same control.

So in my Swift app I’m going to try and replicate it using a box and seg ctrl. this app just needs a visual facsimile of the control, not a real live one. As it is, this apps does GUI displays for macOS, iOS and tvOS, so there are a number of the non-macOS controls I have to “draw manually”

1 Like