Shifting Window Frame without moving its contents in Objective-C - objective-c

I have (+) and (-) buttons... The (+) button should make the window longer in the downward direction, and the (-) button should undo what the (+) button did, by making it shorter from the bottom.
The point is, the frame should only be getting stretched from the bottom and most of the UI items should be staying in place - there are buttons on the bottom of the window that should follow the change in window height...
The problem is I'm using this to shift the window and sustain the same position of its contents...
NSRect frame = sender.window.frame;
NSLog(#"\nHeight and width of window frame: (%f,%f).\nThe x and y origin of the window frame: (%f,%f). ", frame.size.height, frame.size.width, frame.origin.x, frame.origin.y);
frame.origin.y -= 22;
frame.size.height += 22;
[sender.window setFrame:frame display:YES animate:NO];
NSRect viewFrame = mainView.frame;
viewFrame.origin.y -= 22;
viewFrame.size.height += 22;
mainView.frame = viewFrame;
But say you click the (+) button once; all the items shift up slightly while the frame gets longer. If you click (+) consecutively after the first press it will function as expected: the frame will move down and its contents will stay in place.
If you then clicked the (-) button once; all the items shift down slightly while the frame gets shortened. Clicking the (-) consecutively after the first press, it will just move the frame as expected.
It seems pressing the button the first time will move all the windows contents, while consecutive presses will function as intended... I'd like to know if I'm missing something about autosizing in the Interface Editor of XCode. Thank you.

Assuming that mainView is the main view in your window, you should not be modifying its frame manually. So long as you have your springs and struts (or constraints) configured properly, it should resize automatically with the window.

[mainWindow disableScreenUpdatesUntilFlush];
[mainWindow disableFlushWindow];
NSRect frame = sender.window.frame;
NSRect viewFrame = mainView.frame;
viewFrame.size.height += 25;
frame.size.height += 25;
frame.origin.y -= 25;
[sender.window setFrame:frame display:YES animate:NO];
mainView.frame = viewFrame;
[mainWindow enableFlushWindow];
This works fine. I couldn't get the NSView and the window linked so I just move them both manually and use disableFlushWindow to make it happen seamlessly at the same time.

Related

Changing TabView: Resize TabView & Window

I made an application which have a preferences window with 2 tabs.
The first tab have a lots of prefs settings in it, but the second one is very small...
I'd like that the tabview & the window resize when we switch between these 2 tabs.
I act like that, but it doesn't seems to work, when I switch view the "Networks settings" tab is being reduced and disapear (like if the height was going from origin to 0 with animation).
Here is my code (.m):
- (void)tabView:(NSTabView *)tabView
didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
NSRect frame;
int height;
if ([[tabViewItem identifier] isEqualTo:#"Panel settings"]) {
height = 400;
} else if ([[tabViewItem identifier] isEqualTo:#"Network settings"]) {
height = 200;
}
frame = [[tabView window] frame];
frame.size.height = height;
frame.origin.y += height;
[[tabView window] setFrame:frame display:YES animate:YES];
}
Note that I linked the tab view to delegate.
My window is linked to the NSWindow * PrefWindow referencing outlet.
Thanks for your help!
I still think the problem is with the way your springs and struts are set in IB -- make sure you set them for not only for the tab view itself, but also for the individual objects in the view of the tab view item. I noticed that the default settings for my subviews had them all with the top strut set which made any subviews near the bottom of the view disappear when the window shrunk.

ObjC - Resizing and Getting Window Dimensions

In objective C, I'm trying to resize an application window after a button is clicked. If the window was 200x200, a (+) button is pressed to make it 200x210 or the (-) button is pressed to make it 200x190.
I'm just not sure how to set the size of windows after they're made in the editor.
Don't have sample code because it's simple and I'm not sure where to start.
Here you go:
- (IBAction)plusButtonClicked:(NSControl *)sender
{
NSRect frame = sender.window.frame;
frame.size.height += 20;
[sender.window setFrame:frame display:YES animate:YES];
}
Depending whether you want the top or bottom of the window to expand, you may also need:
frame.origin.y -= 20;
Have a look at setFrame:display: in the NSWindow Class Reference.

make buttons stick to side when view rotates

I have a view where I in manually in my code add 2 buttons as subview and place them at a specific location (lower right corner).
How do i make them "stick" to the lower right corner when the view is resized? (on rotation)
Thanks
Did you try to set the autoresizingMask of your buttons ?
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
It assumes that the autoresizesSubviews property of the button's superview is set to YES (that is the default value)
You should implement method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
in your UIViewController and manually change the frame of the button, changing the origin. i.e:
CGRect frame = button.frame;
frame.origin.x += 30;
frame.origin.y -= 20;
button.frame = frame;

NSWindow setFrame | setPosition on the screen

In my application, I need to display a multiple window of same NIB, at a time, and it will be closed/released on the timer basis,
everything is working fine, except the window position. My requirement is, it should display the window exactly below the previous window, I am able to calculate origin for the new window if any window of same NIB is present.
I am using this function to get the origin:
-(void)awakeFromNib{
NSString *eventMsg = [NSString stringWithFormat:#"%s is set “,eventName];
[pTextField setStringValue:eventMsg];
[pWindow setFrameOrigin:[self pointstoDisplayScreen]];
/* On timer callback , i will show the fadeout and fadein effects*/
[self startTimer];
}
/* This method returns the coordinate where to
draw the window
*/
-(NSPoint)pointstoDisplayScreen{
NSRect frame = [[NSScreen mainScreen] visibleFrame];
NSRect windowRect = [pWindow frame];
CGFloat yCoordinate = frame.size.height-windowRect.size.height;
NSPoint point = NSMakePoint(frame.size.width - windowRect.size.width, frame.size.height-windowRect.size.height);
/* Let there be some gap, if any window present and let it draw below to
the existing window
*/
point.y -= (windowRect.size.height + windowOffset)*noOfWindow;
NSLog([NSString stringWithFormat:#"PositionToDisplayScreen x = [%f] y = [%f]",point.x,point.y ]);
return point;
}
The problem is if if previous window is present it draws the new point, slightly below and toward right side of the existing window,
Is there any property i need set, the problem is, if the previous position is exactly on the top right, then it draw the new window on the opposite corner.
[NSWindowController setShouldCascadeWindows:NO];
By setting this, its working, Now all the notification drawing at the top right corner of the screen.
Thanks.
Your window location is "jumping" because you are not doing any bounds checking to determine that the calculated new origin is within the screen bounds.
The class NSScreen will give you details of the screen(s), for each on you cant get its frame.
Now you need to decide how you want a window origin to move; e.g. to the screen edge and no further, onto an adjacent screen if there is one, wrapping (off right means come on left), etc. The choice will depend on your application.
Armed with the screen frame(s) and the movement algorithm you can calculate your new origin.

Cocoa auto-resizable window

How can I make my NSWindow with NSTabView smoothly resize when user clicks a tab? I want it to like "System Preferances" application: window changes its size according to content.
Use NSWindow's setFrame:animated: method. If you want to resize the window down, make sure you decrease the y coordinate of the origin by the same amount you increase the size of the window. To also resize the views in the window, make sure you set up their autoresizing properties correctly.
NSWindow *window;
CGFloat widthChange, heightChange;
NSRect frame = [window frame];
frame.size.width += widthChange;
frame.size.height += heightChange;
frame.origin.y -= heightChange;
[window setFrame:frame animated:YES];