Automatically resizing subview created programmatically - objective-c

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.

Related

iOS9 - UITableViewCellContentView is covering up Controls inside Cell

I have made a custom UITableViewCell called "SwitchCell" that has a switch.
In iOS9 Only, using Xcode 7 beta, the Content view in the cell is on top of the switch. (See screenshot of View Hierarchy. You can clearly see that the content view of the cell is on top of the other views. ):
So all the touches to the UISwitch are intercepted, and the IBAction does not fire.
In iOS8, this is not a problem. See screenshot for iOS 8.4 simulator. You can see that there is no content view on top of the controls:
Has anyone had this problem?
I tried remaking the NIB from scratch, but the same result occurs.
My NIB is a freeform size view with No status bar. It has two outlets: one for UILabel, one for UISwitch.
EDIT: please make sure to check the answer below that asks to verify that the cell's root view is not just a UIView but a UITableViewCell. This issue may also be a side effect of this.
After more investigation and searching, i found my solution here:
Button in UITableViewCell not responding under ios 7
What fixed it for me was:
cell.contentView.userInteractionEnabled = NO;
This prevents the cell content view from taking over the touch events, even though it's on top of the other views.
This issue was not only happening on iOS9, but on iOS7 as well. In iOS8, the Content view was behind the controls.
Problem can be in .xib file for your cell. When you create cell in separate .xib, be sure to drag UITableViewCell on canvas, not UIView.
SWIFT
I had an issue where my buttons in my custom tableviewcell swift files were working just fine, but then I upgraded to Xcode 12 and then all of a sudden I couldn't access them anymore (meaning my taps were not being recognized). The cell content view seemed to be interfering in the hierarchy and this like saved me:
cell.contentView.isUserInteractionEnabled = false
I put the line in cellForRowAt.
Thank you to #FranticRock
I had this problem when I was reusing a cell as a .xib. I didn't realise but when I first created the xib the default view that it created was in fact a UIView and not a UITableViewCell. It seems that at some stage UIKit adds the content view on top of my other elements and therefore interrupts certain events (e.g. touch events).
I resolved this by opening my xib file and dragging a UITableViewCell onto the canvas and copying my UI elements from the old view to the new cell.
Afterwards, additional settings also became available in the attributes inspector that matched those for a UITableViewCell.

Alternating between several NSViews

What I need may be pretty basic, but I'm definitely not sure as to how to proceed (I've done that before but none of my choices seem that Cocoa-friendly).
Ok, let's say we've got 2 NSViews - one next to the other:
The one on the left serves as a menu.
The one on the right will show a NSView (from a different XIB perhaps?) depending on the selection on the menu.
My questions :
How should I go about loading the different NSViews into the rightmost NSView?
How do I make sure that the subview (the one currently active) is properly resized when the window is resized?
rdelmar's solution should work, but another approach, which may be simpler, is to use an NSTabView to handle switching between the content views. You can hide NSTabView's tabs using the settings pane in interface builder, or by calling [self.tabView setTabViewType:NSNoTabsNoBorder]. I'd probably use a table view for the left side. When the user selects a row, you do something like:
-(void)tableViewSelectionDidChange:(NSNotification *)notification
{
[self.tabView selectTabViewItemAtIndex:[self.menuTableView selectedRow]];
}
The NSTabView can/will take care of properly resizing its content views as long as you've set up its and its content views' autoresizing masks (springs and struts) properly.
You should be able to create a custom view in IB that looks like your yellow view, and set its resizing behavior to expand in both directions with window resizing. Then, when you get your new view (by just referencing one you already have or loading a new xib), add it as a subview of the custom view, and set its frame to that of the custom view. I think that views resize their subviews by default, so it should resize correctly with the custom view.

Setting subview's width equal to window width on launch

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.

How to make controls "tabable" when loaded from plugin in Cocoa app?

I have an application thad loads in plugins that have their own UI.
There is an IBOutlet called ContainerView in my AppDelegate.
When the plugin loads, it puts its own view (that is stored in a xib in the plugin bundle) into the Container view like so
[ContainerView addSubview:viewFromPlugin];
When the view loads, everything is fine but when I press tab the only controls that get any focus are ones outside of the ContainerView and none of them inside it get focus.
I've tried setting the container view as the initialFirstResponder and I've tried hooking up the nextKeyView from the last button in the tab order to the ContainerView.
Thanks.
I've tried setting the container view as the initialFirstResponder and I've tried hooking up the nextKeyView from the last button in the tab order to the ContainerView.
Is that what you want to do? Make the container view the key view? It doesn't sound like it; it sounds like you want the plug-in view (or a view within it) to become the key view.
This section of the Apple documentation explains all of the different aspects of the key view loop. I believe you'll want to either set the nextKeyView of the container view as well as the view before it, or override the container view's nextValidKeyView method—possibly both. You might also try setting the previous view's nextKeyView directly to the plug-in view's nextValidKeyView, skipping over the container view entirely.
Did you try calling the becomeFirstResponder method on your ContainerView?
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/becomeFirstResponder
UIView classes inherit from UIResponder.

Cocoa HUD window - how to turn off topmost?

I've wrote some small cocoa app and it's main window has HUD style.
The problem is - when I set HUD style Interface Builder automatically also sets Utility style - which make the main window topmost (always visible over every other windows). Is there a way to get HUD style panel/window but without making it topmost?
As it turns out - there's a pretty simple solution for my topmost problem:
[hudPanel setLevel: NSNormalWindowLevel];
Makes it act like a normal window that isn't topmost.
If you can't do it in IB, you'll have to do it programmatically. In this case, that means creating the window programmatically. (You'll want to move the window's views into a separate top-level view in the nib, and set that view as the programmatically-created window's content view.)
You should also file a bug report, as it doesn't seem from the NSPanel documentation that the HUD style necessarily implies the utility-window nature.