Create NSView directly from code - objective-c

I would like to run a small program that opens couple of views. I do not want to use any xib files definition except of Basic AppDelegate.
Can someone direct me to any example of how can I open new window in Cocoa without defining it in xib file, just from the code?
This is what I am doing right now - what should I add to it?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSRect frameRect = NSMakeRect(100, 100 , 256, 256);
NSView* tmpView = [[NSView alloc] initWithFrame:frameRect];
[tmpView setHidden:NO];
[tmpView setNeedsDisplay:YES];
}
Thanks!

You need to create a new NSWindow and set its contentView to your new NSView like so:
NSWindow *myWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(100,100,256,256) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
[myWindow setContentView:tmpView];
[myWindow makeKeyAndOrderFront:self];

Related

How to Call awakeFromNib in mainViewController.m

I have an xib file containing a custom cell. I'm trying to access the height of an object created in customCell.m.
Here is my code:
customCell.m
- (void)awakeFromNib
{
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setText:#"This is a label"];
[self.myView addSubview:self.label];
NSLog(#"%f", self.label.frame.size.height); // Results: 200.0000
}
mainViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
customCell *cellVC = [[cutsomCell alloc] init];
NSLog(#"%f, %f", cellVC.label.frame.size.height); // Results: 0.0000
}
awakeFromNib is called when all file owners outlets and properties are set. Things are not wired up (in terms of frames/layout) in viewDidLoad.
awakeFromNib is called after viewDidLoad, that is why you see the difference.
Sorry, this doesn't work. You are creating your customCell via alloc/init in code. AwakeFromNib is only called for objects defined in a nib file.
You probably need to define your customCell in a separate nib file, and load that nib file through + (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner or similar.

Custom Window Style in Cocoa

OK, this is what I'm trying to do :
I have a custom NSPanel subclass
I want the NSPanel to be borderless (NO title - I'm drawing a titlebar myself) AND resizeable
The thing is that :
once I set the styleMask to NSResizableWindowMask, the default title bar appears as well.
once I set the styleMask to NSBorderlessWindowMask, the default title bar disappears (that's good), but the window loses its resizing ability.
This is my Code :
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
if ((self = [super initWithContentRect:contentRect styleMask:NSTitledWindowMask backing:bufferingType defer:deferCreation])) {
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setMovableByWindowBackground:YES];
[self setLevel:NSFloatingWindowLevel];
//[self setStyleMask:[self styleMask]&~NSTitledWindowMask];
}
return self;
}
As you may see from the commented-out code, I've tried using any possible combination of bit operations with the mask so that I combine what I need.
Any ideas??
Just do them at a time like this
styleMask:NSTitledWindowMask | NSResizableWindowMask

How can I make a fullscreen overlay on the OS X desktop?

I want to make some kind of drawable surface that exists beneath the mouse cursor but above everything else rendered on the desktop. I am trying to create a "trail" behind the mouse.
How can I do this in Cocoa and Objective-C?
You need to subclass NSWindow to create a borderless window and set its window level to something like NSScreenSaverWindowLevel - 1.
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(NSUInteger)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
self=[super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:bufferingType
defer:flag];
if(self!=nil)
{
[self setHasShadow:NO];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setLevel:NSScreenSaverWindowLevel - 1];
}
return self;
}

ViewController view starts with at ~(0,20)

I've tried starting view based app and window based app but I still have the same problem.
When I add a navigation bar it looks like it starts at cords ~(0,20) and leaves some space between the it and the "system tray" (i can't find the word for the dock with clock and batteri etc.)
i can't figure out why this occurs because the nslog of frame tells me that it origins in (0,0) (and strangely the width is 0 :/ but it is clearly visible).
code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y+48.0f,
self.view.frame.size.width,
self.view.frame.size.height)];
[image setImage:[UIImage imageNamed:#"IMG_0792.PNG"]];
[self.view addSubview:image];
self.navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
45.0f)];
[navBar setDelegate:self];
[navBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle:#"Image Stream"] animated:NO];
[self.view addSubview:navBar];
}
return self;
}
and in the appdelegate i just declare it, retain-property and:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.menu = [[MenuViewController alloc] initWithNibName:nil bundle:nil];
[self.window setRootViewController:menu];
[self.window makeKeyAndVisible];
return YES;
}
would be very nice if someone knew why this happens. thanks
When positioning a view using the coordinates of the superview, use bounds, not frame. If there's a status bar, your superview.frame.x will be non-zero, but the subview's coordinates are relative to the superview, not the position.
Check the following:
In your UIViewController's Xib if it has status bar. If yes, then put None. (You can set that in the inspector, just select your root UIView.
Are you adding your UIViewController to your App delegate's by XIB? If yes, go check in the App's delegate xib your UIViewController and see if it has status bar.
p.s: its called status bar.
Edit what about the frame you use for your UINavigation? The 45.0f.

ModalViewController for Single Subview

Ok, so bear with me: as this is an Objective-C related question, there's obviously a lot of code and subclassing. So here's my issue. Right now, I've got an iPad app that programmatically creates a button and two colored UIViews. These colored UIViews are controlled by SubViewControllers, and the entire thing is in a UIView controlled by a MainViewController. (i.e. MainViewController = [UIButton, SubViewController, SubViewController])
Now, all of this happens as it should, and I end up with what I expect (below):
However, when I click the button, and the console shows "flipSubView1", nothing happens. No modal view gets shown, and no errors occur. Just nothing. What I expect is that either subView1 or the entire view will flip horizontally and show subView3. Is there some code that I'm missing that would cause that to happen / is there some bug that I'm overlooking?
viewtoolsAppDelegate.m
#implementation viewtoolsAppDelegate
#synthesize window = _window;
#synthesize mvc;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mvc = [[MainViewController alloc] initWithFrame:self.window.frame];
[self.window addSubview:mvc.theView];
[self.window makeKeyAndVisible];
return YES;
}
MainViewController.m
#implementation MainViewController
#synthesize theView;
#synthesize subView1, subView2, subView3;
- (id)initWithFrame:(CGRect) frame
{
theView = [[UIView alloc] initWithFrame:frame];
CGRect sV1Rect = CGRectMake(frame.origin.x+44, frame.origin.y, frame.size.width-44, frame.size.height/2);
CGRect sV2Rect = CGRectMake(frame.origin.x+44, frame.origin.y+frame.size.height/2, frame.size.width-44, frame.size.height/2);
subView1 = [[SubViewController alloc] initWithFrame:sV1Rect andColor:[UIColor blueColor]];
subView2 = [[SubViewController alloc] initWithFrame:sV2Rect andColor:[UIColor greenColor]];
subView3 = [[SubViewController alloc] initWithFrame:sV1Rect andColor:[UIColor redColor]];
[theView addSubview:subView1.theView];
[theView addSubview:subView2.theView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton addTarget:self action:#selector(flipSubView1:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[aButton setFrame:CGRectMake(0, 0, 44, frame.size.height)];
[theView addSubview:aButton];
return self;
}
- (void)flipSubView1:(id) sender
{
NSLog(#"flipSubView1");
[subView3 setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[subView1 presentModalViewController:subView3 animated:YES];
}
SubViewController.m
#implementation SubViewController
#synthesize theView;
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)color
{
theView = [[UIView alloc] initWithFrame:frame];
theView.backgroundColor = color;
return self;
}
TLDR: modal view not working. should see flip. don't.
It doesn't look like you're setting the 'view' property of the MainViewController anywhere, just 'theView'. The controllers view delegate must be connected to the root view it displays for it to work properly. You'll need to correct that on the Sub View Controller impl as well. If you want all the plumbing that framework classes bring, you have to set things up the way they expect.
Also, you're calling presentModalViewController on one of the sub view controllers; change that to call [self presentModalViewController:...], since the MainViewController is the one which will 'own' the modal view.
I think if you fix those points, you'll find -presentModalViewController will work.