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];
Related
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.
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.
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.
How do i get the current size of the screen no matter the resolution change in xcode so that i can adjust my NSRect size to be maximized or take up the screen according to the resolution ? What do I need to add? Would it be something to the NSWindow?
NSRect panelRect = [[self window] frame];
panelRect.size.height = _HEIGHT;
panelRect.size.width = _WIDTH;
panelRect.origin.x = 0;
panelRect.origin.y = 0;
[[self window] setFrame:panelRect display:NO];
Thanks!
You can use CGDisplayBounds, which returns a CGRect containing the screen's bounds (origin and size) in global coordinates.
You can use CGDisplayRegisterReconfigurationCallback to be notified when a display configuration changes, which includes when its resolution is changed.
Quartz Display Services Reference
Have you tried using [[NSScreen mainScreen] frame]? If you want to exclude the menubar, you can subtract the menubar height from the NSRect's height. (It's around 20 pixels)
Edit: If you want your window to be resized from the start, you can put the method above in your controller's awakeFromNib and use NSWindow's setFrame:display: to resize it.
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;