Getting NSView to appear programmatically in Objective-C - objective-c

I have a NSView object that is being returned to me as a result of a function. I know the view is valid because I can see the contents of the view if I do this:
NSRect rect = NSMakeRect(600,600,200,200);
NSWindow *testWindow = [[NSWindow alloc] initWithContentRect:rect styleMask:NSTexturedBackgroundWindowMask backing: NSBackingStoreBuffered defer:NO];
[[testWindow contentView] addSubview:returnedView];
[testWindow makeKeyAndOrderFront:NSApp];
In my application I have a window with a custom view (has some text on it) that has an outlet referenced in my code using IBOutlet. I'm trying to add the view I'm getting returned as a subview of that outlet.
[referencedView addSubView:returnedView]
[referencedView setNeedsDisplay:YES];
The referenced view is visible (I can see the text in it), but the returnedView doesn't appear on top. Am I forgetting something?
This is what my code looks like now:
[returnedView setFrame:NSMakeRect(0,0,200,200)];
[referencedView addSubview:returnedView];
[referencedView setNeedsDisplay:YES];
[referencedView drawRect:[referencedView bounds]];

Views can only be in ONE superview is what I just learned. I had the test code and the code I wanted to work so it was removing my view and putting it in the window instead.

That custom view (with the text in it) has a custom drawRect implementation, in which the text is drawn, right? In this case, my idea is that you'd want to call super's implementation of drawRect to make sure that the subviews get drawn too.

Related

How to add UIView as a subview on UIViewController in UIStoryboard

Is there any way that I can add uiview as a subview over a view controller using UIStoryboard, using xib's we can do that, but i'm unable do that using storyboard.
My storyboard is not holding any of the uiview as a subview when I drag and drop on it, it was placing under the view controller.
Is there any way that I can add it programmatically on my view controller using storyboard.? I'm stuck please help me out
Am I right, you want to hold UIView in storyboards without view controller or superview ?
You can't do that. You should use XIBs to hold custom views.
It doesn't matter you add it programmatically or via drag and drop, in storyboards you can't hold "isolated" views, every view must have a superview and therefore a UIViewController.
Check apple's guide, make sure you understand UIViewController,UIView,UIStoryboard classes and relations between them. Also this.
Hope it helped.
Yes, you can override UIViewController's loadView method to do it as i have written code below.
Because loadView is the method which is called first of all other viewController's loading methods. So you can set it here.
Hope this will work for you as I have tested it on my code.
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
self.view.backgroundColor = [UIColor blackColor];
// enter your customization code here
}
write this line in function
-(void) ViewDidLoad:
[self.view addSubView:...];

frame of Root ViewController's View when loaded programmatically

Im loading a root view controller in landscape mode at launch(no interface builders are used).
In viewDidLoad, I am adding subviews to root view controllers view, like this
- (void)viewDidLoad
{
[super viewDidLoad];
// self.view.
UIView *toolBar=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
toolBar.backgroundColor=[UIColor darkGrayColor];
[self.view addSubview:toolBar];
//code contiues...
}
but self.view.frame.size.width returns width of portrait mode instead of landscape.
thanks in advance
EDIT:
Implement the -loadView method:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
Also normally the parent view controller is responsible for setting the frame size of its children view controllers views. In the case of the root view controller, I believe it takes the size of the UIWindow it's attached to (so if you set the window size in you app delegate, you can just use [[UIView alloc] init] and then set the autoresizing mask in the -loadView method).
You might need to take the status bar into account in the above code, depending on your own code.
may be, portrait orientations are first in your project file (click to project in xcode, info tab in target, unit Supported interface orientations) ? If so, your app can launched in portrait orientation, then send viewDidLoad and rotate to landscape only after this.
When you do not use Interface Builder/xib files and neither create your view manually within loadview, the systems creates one with the maximum dimensions. This is stated in the loadview method documentation:
If the view controller does not have an associated nib file, this
method creates a plain UIView object instead.
This system generated UIView object has a transform property that is not the identiy transformation and thus you are not allowed to rely on the values from the frame property as stated here:
http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/transform
Warning: If this property is not the identity transform, the value of
the frame property is undefined and therefore should be ignored.
So, what should you to instead? I guess the best solution is to use the bounds property of you UIView. This contains the already rotated coordinates.

Adding Subviews to NSView show up, but can not be removed

In my application window I have two NSViews. On the left the NSView ("Menu") contains a few buttons. When one of the buttons is clicked it should change the contents of the right NSView ("Content").
For each of the views on the right I have a separate NSViewControllers that get loaded and their views gets added as a subview. When a further button gets pressed on the left the added subviews on the right should be removed and the new view should be loaded as a subview.
To accomplish this I load my Menu in AppDelegate with the following:
MenuVC *menuSubView = [[MenuVC alloc] initWithNibName:#"MenuVC" bundle: nil];
menuSubView.contentView = (NSView*)[self contentView];
[[self menuView] addSubview:[menuSubView view]];
This works fine. As you can see I have a NSView pointer in the Menu VC which points to the contentView so that I can populate it with the subviews.
Now as a method for one of the button presses I do the following:
SomeContentVC *subView = [[SomeContentVC alloc] initWithNibName:#"SomeContentVC" bundle:nil];
[self.contentView addSubview:[subView view]];
This does not work.
If I however add a subview from the awakeFromNib method of the MenuViewController implementation (in the case of default content when the app opens) it works. However when I try to remove that subview using
[[self.contentView setSubviews:[NSArray array]];
I can't. Interesting is also that if I try to count the number of subviews (even after having added one in the awakeFromNib method) it returns 0 subviews for self.contentView. Why? How can I get it to work properly?
Thanks
The fact that messaging self.contentView achieves nothing except, for some things, returning 0 probably means that self.contentView is nil.
Do you perhaps have two instances of MenuVC by accident? Perhaps one instantiated in a NIB and one instantiated in code?
When in doubt, log everything. Log self in various methods. Log menuSubView just after you create it. Log menuSubView.contentView just after you assign it. Etc. Eventually, you'll probably see that you're interacting with different objects than you thought you were.

NSView mouse tracking

I'm facing a strange behavior with Cocoa NSView on Mac OS X.
I've a custom NSView in a NSView container, this custom NSView tracks mouse movements, clicks, and has a tooltip.
When I add a NSView above the described view, I can still see the tooltips even if the view with the tooltip is under, behind and not visible.
I'm pretty sure that I misunderstood something in the event handling chain. Any help is really appreciated!
The core issue is that you are not supposed to have overlapping views in Cocoa. Or at least, the behavior then becomes undefined. A view can be a subview of another view, but not simply a sibling within the bounds of the other view.
However, one way to solve your particular problem is to make the view underneath hidden, using the setHidden: method.
If you're not using it anymore you can call the removeFromSuperview method.
NSView *myView
[myView alloc] init]
// do stuff
[myView removeFromSuperview]
if you just don't want it to receive events you can call the resignFirstResponder method
NSView *myView
[[myView alloc] init]
// do stuff
[myView resignFirstResponder]

tableView footer strange behavior

Given: I have a bit of a "pop up" view that I put over my tableView within my UITableViewController. I put it there like this:
[self.navigationController.view addSubview:self.hoverView];
Problem: I cannot see this hoverView when I add a tableView footer view. Seemingly unrelated yes?
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.tableView.tableFooterView addSubview:someLabel];
These appear to be mutually exclusive. I can have one, but not the other. To see what's going on, I print subview descriptions like so:
for (UIView *sub in self.navigationController.view.subviews)
{
NSLog([sub description]);
}
When the table footer view is added, this code prints nothing. Otherwise, I see the expected output of some navigationController internal views. What did I miss?!
Don't add the hoverView to the UINavigationController's view. Instead of using a UITableViewController, use a UIViewController whose view contains a UITableView as a subview. (See here for details of how to implement the rest of the UITableViewController's functionality in your UIViewController). Then add your hoverView to that view, i.e., make it a sibling of the UITableView, but a later sibling so it appears above it.
This may not solve your problem but I'd say it's your best bet: a UINavigationController is not designed to have its view manipulated directly, it's designed to have view controllers pushed onto its stack and add subviews to its view accordingly.