1

I'm having some problems with displaying an image in a window that I moved away from the Main storyboard into its own xib file.

I created a LoginWindow.xib for a window that I need to display, and I initialize its view controller using the regular

self.loginViewController = [[LoginViewController alloc] initWithWindowNibName:@"LoginWindow"];
[self.loginViewController showWindow:self];

In the window xib I connected the file's owner window delegate to the window so all my code from - (void)windowDidLoad in the LoginViewController works perfectly, the whole thing.

However, I have an NSImageView that should displayed in the window. But the image is displayed in the interface builder, but NOT in the running app and I have no idea why.

On the screenshot below you can see the image has the correct image name (from the "Assets.xcassets" file), and is displayed in the Interface Builder, but not in the running app.

(Note: The gradient view below the image is a custom view that overrides drawRect to create a gradient)

I've also used the UI debugger to see if the image is displayed below the gradient view, but no success. The strange thing is that the image even is showed in the UI layers debug window thingy (screenshot 2 below)...

I'm probably missing a simple checkbox or value that needs to be set or connected, since all this worked perfectly before I moved the window away from the Main.storyboard file and into a separate XIB file...

Does anybody have an idea how/why? I've also already cleaned the whole build directory, closed and re-opened xcode, ... you name it.

Interface Builder screenshot

UI Debugger screenshot

brtdv
  • 157
  • 1
  • 2
  • 13
  • At the end of viewDidLoad try `[self.view bringSubviewToFront:yourImageView]` – vp2698 Jun 24 '17 at 11:43
  • Hi vp2698. Thank you for the suggestion. Sorry for the confusion: the `LoginViewController` is actually a subclass of `NSWindowController` and therefor uses `windowDidLoad`. I'm not sure if I can use `[self.window bringSubviewToFront...]`? I know, the naming is weird and my class should be renamed to `LoginWindowController`... So in `windowDidLoad`, would that become: `[self.window.contentView bringSubviewToFront:yourImageView];` ? You can also see in the screenshot at the bottom that when I debug the layers of views, the image should *already* be at the top...? – brtdv Jun 24 '17 at 11:48
  • Try this https://stackoverflow.com/questions/2872840/how-do-i-make-an-nsview-move-to-the-front-of-all-nsviews – vp2698 Jun 24 '17 at 11:53
  • Can you add your `windowDidLoad` code? – vp2698 Jun 24 '17 at 12:00
  • See [this screenshot](https://imgur.com/a/sZb3J). Thanks, using your suggestions it all works now, but I'm still trying to understand why the running app doesnt display the views in the same order as interface builder? – brtdv Jun 24 '17 at 12:08

2 Answers2

1

I used the suggestions of vp2698 to get this working and adding the following lines to my windowDidLoad method worked:

[self.logoImageView removeFromSuperview];
[self.window.contentView addSubview:self.logoImageView positioned:NSWindowAbove relativeTo:nil];

(logoImageView is an outlet to the NSImageView with the image)

So: thanks vp2698!

So yeah: it works now... But might anyone explain why this did not work straight out of Interface Builder?
Why exactly do I need to re-add the image "above" all other views, while I already positioned it above everything else through interface builder? (Something that the "Debug View Hierarchy" screenshot also confirms...?)

It's great that I got this working but I'm really also trying to understand the cause of the problem. Thanks!

brtdv
  • 157
  • 1
  • 2
  • 13
  • I think in somewhere when you making gradient view. you are re-adding the view to `window` – vp2698 Jun 24 '17 at 12:17
  • [This is the code for my gradient view](https://imgur.com/a/8HuhY). Should I optimise this some way? Why is the image displayed above the gradient in the "View Debug Hierarchy"? Or is this debug tool considered unreliable? – brtdv Jun 24 '17 at 12:25
0

From your description image has the correct name (from the "Assets.xcassets" file) but you have to check if your image has all scales. If you have only one image for 3x and missed 1x and 2x it will not work. Also look in the logs, you will see the issue like: "image doesn't exist."