I'm failing in creating a simple macSO app with Xcode 9 because of a very ... and I mean very strange behaviour. I have two views with simple layers to draw different background colours. Like this:
When I run the app, without changing anything, after (usually) the third run in a row, the views disappear ... literally. And the window looks like this:
Here is a test project:
https://github.com/ivailon/test13
Furthermore - if I start executing the already build app, again the views sometimes disappear. Can anyone give even a clue of a reasonable explanation why this is happening ...
The problem is that not every time the view's layer is initialised before the call of:
func viewDidLoad()
Description
Called after the view controller’s view has been loaded into memory.
To fix this one should initialise the layer later or create its own CALayer object and setup the view with it, like this:
let layer = CALayer()
layer.backgroundColor = NSColor.orange.cgColor
view1.layer = layer
view1.wantsLayer = true
The project has been updated at GitHub to show the difference.
Related
I'm having some trouble in my current project. I am trying to replace a view by calling removeFromSuperview() on the old view and then adding the new view using addSubview().
The new view that's coming in is simply a layer-hosting NSView with the contents of the layer set to some image.
This works fine most of time, but when the image happens to be large (42MP loaded from a .arw file in my case), it takes several seconds from execution of the following replacement code to the views actually being visibly replaced.
oldView.removeFromSuperview()
view.addSubview(newView)
Any ideas as to what might be causing this are highly appreciated.
Please note this is not an iOS question.
I have an NSView-based app (i.e. not document-based), and I’d like to bolt on a printing subsystem. I can get NSViews in my main controller to print ok. However, I want to have a special view constructed just for printing. The view should not show in the app’s window. The view contains two NSTextFields, two NSTextViews, and 5 labels.
I cannot seem to figure out a way to do this. I have tried various forms of these examples:
Add an NSView to my main view window? Seems logical, but it’s awkward in a storyboard, (I can’t position the view in the storyboard).
Programmatically create a custom NSView with a xib?
For this, I’ve tried:
#IBOutlet weak var printView: NSView!
….
let printOperation = NSPrintOperation(view: printView!)
This results in the comprehensive "fatal error: unexpectedly found nil while unwrapping an Optional value” message.
The outlets are configured correctly (I think)
A seperate ViewController? If so, how can I avoid having two print buttons — one to call the print controller, and the second, to print the PrintController’s view.
I’ve tried reading the Apple docs, but they are not the way I learn best. There are also no Print documents in Swift that I've found. I’ve waded through many SE questions, but have come up blank. Could you point me towards a solution please.
I think the specific problem here is that you're never causing the view to be loaded.
You can double check whether this is the case by overriding the viewDidLoad method on the controller. If it's never called, your view is never loaded from the nib.
Normally the UI machinery takes care of all that when you display a view controller, which is why it can be so confusing when it doesn't happen.
You should be able to trigger the view load by accessing the view property on the controller. e.g.
_ = self.view // Touch the view to force it to load
https://developer.apple.com/documentation/appkit/nsviewcontroller/1434401-view has some additional information.
You can also call loadView() directly although that's usually frowned upon.
Xcode 7.0.1 (7A1001)
iOS 9.0
I made a custom #IBDesignable UIView, only implementing the drawRect function. Like so:
#IBDesignable
class CustomView: UIView {
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
// Lots more drawing code after this
// ...
}
}
While I was writing this drawRect, I had a storyboard with several different instances of this rendering on a view controller. When I made a change in drawRect and saved, these changes would be reflected in the storyboard in close to real time. There was even an IBInspectable String which allowed me to change an enum type and draw differently depending on that. The views could be resized in the storyboard, and the new width and height would be accounted for in my drawing code, and the drawing would be scaled and translated, exactly as I intended.
This was all working fine, and I had not made any changes to that UIView subclass, but I did delete my test views in the storyboard while building out newer views which would actually end up using this custom view.
After doing a lot of dancing around with constraints, I was ready to place my custom view where it belonged. But it does not work. While the identity inspector acknowledges my custom class as an IBDesignable, it hangs on "Updating" status.
To prove I wasn't going crazy, I made entirely new #IBDesignable UIView subclasses which implemented simple drawRects or even completely empty drawRects. These are now having the same issue and are not rendering on the storyboard.
Note that I can run this in the simulator and the drawing appears as I expect at runtime. So what happened to the storyboard's ability to give that nice feedback I expect from an IBDesignable?
Things I have tried:
Clean / clean build folder / clearing XCode Derived Data directory
Restarting XCode
Deleting / commenting out drawRect guts
Making the most simple #IBDesignables with HelloWorld-like tutorials
Using existing known working #IBDesignables
Creating fresh UIViewController in storyboard to place custom view
Toggled: Editor > Automatically Refresh Views
Editor > Refresh All Views
Editor > Debug Selected Views
All these attempts continue to show "Updating" in Custom Class section of the identity inspector.
Clearly something beyond my implementation is causing a problem here. Anybody else having this issue? Can anybody point me in the right direction? Not sure what else to try.
In the Identity inspector where it says "Module" add your AppName
I'm trying to get the split view controller working properly but apparently there's a bug in there or I'm missing something.
First of all, I've started a blank OSX Obj-C application, and in the Storyboard I've dragged the split view controller. Next, I've linked the segues from the main window controller to the split view and added two labels.
http://i.imgur.com/dlFObaF.png
When I build the project, it shows only the second page. Not to mention strange window size in the final build.
http://i.stack.imgur.com/IqRqr.png
I've tried everything.
This occurs in both vertical and horizontal split view.
Any suggestions?
I had the same issue myself today, but it's just the split line wasn't initiated properly.
To see, this, once you run your app, move your mouse toward the edge of the window and drag it, you will now see another view emerging.
To my best knowledge, I do not know how to fix this in IB or in code. Apparently NSSplitViewController does not have a property like UISplitViewControllerAutomaticDimension. Would appreciate if someone can contribute to this.
Edited: Found an answer via another thread. Basically, try to add some constraints to the subviews inside each view and that should prevent the size of a view to be zero. In my toy example, adding margin constraints to my buttons worked out well.
I'm not certain why this isn't working, but maybe someone here can shed some light on what the problem might be. I am working on game based on the XCode OpenGL template. I have a small GLKViewController with a view that does not take up the full screen area, so I'm creating it inside another UIViewController that does have a full screen view as a container. This works, and my update/draw methods are called as I would expect.
However, when I take my container view (with the GLKViewController inside it) and try to initialize it inside a UINavigationController as the rootViewController, the update method for the GLKViewController never gets called, and the draw methods is called exactly once.
This view hieracrhy works fine if there is no UINavigationController, however as soon as it's added, it does not work. I have verified that the viewDidLoad for the GLKViewController is being called. The context is initialized, because I can see the first frame, but after that it looks like the draw/update messages are being swallowed up. I can get around this by not using a navigation controller for this particular view, but I'd like to understand why this does not work. Any idea on what might be happening to the draw/update functions?
I recently managed to get GLKViewController running inside a Master/Detail view controller and faced this same problem.
Did you check that GLKViewController is not paused?
This would fire the update loop:
self.paused = NO;