Setting subview's width equal to window width on launch - objective-c

I'm struggling getting a subview (which is acting as a custom toolbar [I need a NSView rather than an NSToolbar]) centred within an INAppStoreWindow..
The subview is being added correctly, and stretches correctly only if the window is exactly the same width as the custom view that I've created in IB.
If the window opens wider than the custom view, it does not stretch properly. The window seems to open in exactly the same state as I left it (in Lion), which therefore means I have to set the width of the custom view to the restored window width on launch.
It's also important that the buttons etc I have placed in the centre of the view in the nib remain centred...
How do I do this?
Edit: to make this clear, how do I get a restored window's width? I have set it to 480 in the nib file but if I resize the window, quit then restart the app, window.frame.size.width still returns 480, not the width I quit with..
Many thanks

I take it you are not creating the NSWindow in interface builder, you can override -[NSView viewDidMoveToWindow] or -[NSView viewWillMoveToWindow] to set the width, there are also viewDidMoveToView equivalents which may make more sense since you view gets added to the NSWindows contentView, there is also awakeFRomNib which you can override which may b he better choice if you only have to worry about the issue once when you vie loads from the Nib file.

Related

UiButton click doesnt work on UIView

I have a UIView which is added to Page View Controller. This UIView have a multiple buttons which are tied to an action defined in the controller. At runtime, these views get added to Page View Controller. The problem i am seeing is after loading the page view controller, when i click on the first button. It works fine. But the rest two buttons doesn't. I made sure that the User Interaction is enabled and it is linked to IBAction. Don't know what is causing this issue.
UIButtons are usually unresponsive if their superview userInteractionEnabled property is set to NO or if they are outside of their superview's bounds. The fact that only two of the buttons are not working, makes me think that the latter is the cause of the issue. Set the background color of the superview, run the app and check that the buttons are within their superview's bounds. If they aren't, move them inside the bounds or increase the superview's frame size in order to fit them in.

Is there a function to know the useableSize of a view?

It must take into account:
statusBar (which can be 40 points if you have hot spot)
TabBar
NavigationBar.
Basically at viewDidLoad I see that my view has a size of 320*480.
I wonder where did iOS decide that as the screen size of my screen. I use UIStoryBoard.
So, on viewDidLoad, I intended to resize that.
I am not even sure if this is the right approach.
Note: the issue I am facing doesn't seem to happen if I do not use storyBoard.
At viewDidLoad, when I use XIB, the content of self.view is correct, namely 416, instead of 480, due to UInavigationController and UITabBar
Try overriding the UIViewController viewWillLayoutSubviews method. The view's frame will be set by then.
In the viewWillLayoutSubviews method, the view controller's main view is the size you need to know. It has been adjusted for status bars and nav bars and tools bars and tab bars. It also takes into account orientation. There is no single method where you can ask what the size will be. Besides, there is no need to ask such a question. Create all the subviews you want in viewDidLoad. But lay them out based on the view's size in viewWillLayoutSubviews.

Only subviews added in viewDidLoad automatically resize

I've created a custom subclass of UIViewController that acts like a UINavigationController or a UITabBarController. Let's call it a ToolbarNavController. It has a toolbar at the bottom with controls for the user to move to a different content view.
I have two content views aside from the ToolbarNavController's view. Both are loaded from a nib and have their own controllers. The app starts out showing one of them. A button in the toolbar allows the user to switch between them.
When I add these views as subviews of the ToolbarNavController's views in viewDidLoad, they are correctly resized to fill the area between the status bar and the toolbar without overlap/underlap.
But when I try to lazy load the second view, adding it as a subview for the first time only when the user presses the toolbar button, iOS does not resize the view to account for the toolbar in its parent view, and it underlaps the toolbar which messes up my Autolayout constraints. Also, when I don't add the subview in viewDidLoad, if I put the device in landscape orientation before switching to the second view, it loads with a portrait orientation frame.
Bottom line: When inserting a subview in viewDidLoad, iOS sizes it correctly and manages autorotation for it. When inserting it later, I need to detect orientation set the frame myself. (And for some reason when I do this, autorotation kicks in again).
What is going on?
In viewDidLoad, the view is not yet layout for the resolution and interface orientation, and view properties are as they were in the interface designer. So, if you had a portrait view, that is how the initial properties of the view are set when going into viewDidLoad. When you add your view there, you add it to the XIB view. Later, iOS performs layout on the view hierarchy and thus resizes your inserted view as needed. But when adding your view at a later point, the view hierarchy has already been layout, so it is expected that the new view you are adding is also layout correctly.
Best practice is to calculate the size you need using the size of the view you are inserting into. For example, half the width of the containing view, or third the bounds, etc. This way it is independent on the orientation the interface is in.

Automatically resizing subview created programmatically

I have a NSSplitView. On the left pane of that split view I have an NSTableView and a custom view created programmatically. I'm using a delegate to make sure my two panes don't resize at the same time.
I add my custom view thus:
BWAnchoredButtonBar *anchoredButtonBar = [[[BWAnchoredButtonBar alloc] initWithFrame:[leftPane bounds]] autorelease];
[leftPane addSubview:anchoredButtonBar];
And it seems to work ok. When I run my application everything works fine. Now, my problem is that when I resize the split view pane, the custom view does not resize with it leaving an ugly white space between it and the divider of the NSSplitView.
I guess what I want to ask is, how can I programmatically set the springs and struts that IB sets visually?
Also, I can't use IB because Xcode 4 does not support IB plugins.
-[NSView setAutoresizingMask:] is how you set springs and struts programmatically.

NSScrollView incorrect content size

I have an NSScrollView that is the parent of a custom NSView subclass. The subclass uses the NSScrollView's contentSize method in order to layout its subviews.
The issue is that upon first launch, NSScrollView reports the contentSize wrong. It reports the size as being 15px more than it should be (the width of the scroller). So it seems to me that it is returning the contentSize without taking into account the scroller width; however, as soon as I adjust the frame of the scroll view (by resizing, etc.) the content size is reported properly. It seems to be just a problem upon initial launch.
Should this be reported as a bug, and are there any good solutions to this? I could use the dirty way of performing a check during layout to see if its the first time the method has been called, and then deduct 15px from the content size, but if there's something better, that would be appreciated.
I just came across a similar problem and the way I solved it was to observe the notification NSViewFrameDidChangeNotification on the Scroll view's content view (in your case the custom NSView).
This notification gets triggered when a scrollbar is added/removed from the scroll view, at which point you can reposition your view's content.
When are you checking the content size? Is it happening as your view is being instantiated from the nib file, or after the nib has been fully unarchived?
What I would probably do is invoke your layout method from -viewDidMoveToWindow: or -awakeFromNib
Are you using the option to hide/show automatically the scrollbars?