How Do I Add Subviews to an NSTableHeaderCell? - objective-c

I have been experimenting with customizing NSTableViews and have gotten onto NSTableHeaderView and NSTableHeaderCell now.
I have a simple need. Each NSTableHeaderCell I want to plant my custom view onto. Which simply consists of two buttons.
Here is some code:
I have an NSTableHeaderCell category which simply changes the background color with and without highlight.
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[[NSColor blueColor] setFill];
NSRectFillUsingOperation(cellFrame, NSCompositeSourceAtop);
}
-(void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[[NSColor greenColor] setFill];
NSRectFillUsingOperation(cellFrame, NSCompositeSourceAtop);
}
Next, I have an NSViewController subclass, named NSTableHeaderViewController. This has a xib which I put my buttons into.
Lastly, in my main view, I set the NSTableHeaderView with specified custom height, and add my buttons view to it.
APTableHeaderViewController *tableHeaderVC = [[APTableHeaderViewController alloc] initWithNibName:#"APTableHeaderViewController" bundle:nil];
NSTableHeaderView *tableHeaderView = [[NSTableHeaderView alloc] initWithFrame:NSMakeRect(0, 0, 120, 88)];
[tableHeaderView addSubview:tableHeaderVC.view];
[self.tableView setHeaderView:tableHeaderView];
This works partly as expected, creating my 120 pixel high table header with blue backgrounds, green on highlighting. However it only adds my buttons view, NSTableHeaderViewControlelr, once in the header at 0, 0.
I kind of expect this, but what is the difference between the header view and header cell?
Also, if I want my buttons view to be in each cell, how can I add it to each?
Thanks.

Related

UITableView does not respond to touches at transparent areas

I have a UITableView with a transparent backgroundColor, and the cells inside are initialized with the following code
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(self.contentView.frame), 180)];
self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.label.text = #"test";
[self.contentView addSubview:self.label];
}
return self;
}
Each row has a height of 200, and the label doesn't fill up the entire cell. There will be transparent portions in between each cell. When I try to scroll the table by touching those transparent portions, the touches are completely ignored. I am aware that as of iOS5, touches on views will be ignored. What can I do to solve this?
Things I've tried that don't work:
Setting a transparent or hidden or alpha = 0 UIView to act as a fake background of the tableView
Same deal, touches are ignored.
Creating a subclassed UIView on top of the tableView, and the subclassed UIView uses tableView as the nextResponder
Apparently UITableView doesn't use touchesBegan/cancelled/ended/moved, so this doesn't work. I don't think it's wise to implement my methods to scroll the UITableView either.
Setting the backgroundColor of the tableView to [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]
I don't want to do this, it's still visible.
In short, what can I do to scroll the table even when I start the scrolling from the transparent section?
First you create a label with no text(or a view) such that it completely fills the cell. Then create a new label with your text and add this as a subview of the first view.

How to change the Title View of the navigation bar into some Custom View and fill the original size

I want to change the titleView of navigationBar with a regular text field. Then I want to set the textField size to fill the old "normal" titleView.
I can't seem to be able to do this in storyBoard. Dragging a text Field to the place where the navigation title View is doesn't work.
So I added stuff at
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
PO(self.navigationItem.titleView);
CGRect theFrame= self.navigationItem.titleView.frame;
self.navigationItem.titleView=self.searchBar;
//self.searchBar.frame = theFrame;
while (false);
...
It's working with one cached. That PO is a macro that print the content of the object. Turns out at viewDidAppear, self.navigationItem.titleView is null.
So while I can display the searchBar, I cannot make the searchBar "fill" it's space because I do not know the space is.
I prefer not to hard code it because you know, things may change in the future.
So what should I do?
I once saw codes where rather than setting the self.navigationItem.titleView, you would simply add subview to it. The problem with this approach even on viewDidAppear, self.navigationItem.titleView is 0.
I added these codes:
CGRect theFrame= self.navigationItem.titleView.frame;
CGRect theFrame2 = self.searchBar.frame;
CGRect theFrame3 = self.navigationController.navigationItem.titleView.frame;
And, I do not know how to nslog structure value, however, theFrame and theFrame3 are all 0
You can try this inside viewWillAppear:
UIView *customTitleView = [[UIView alloc]initWithFrame:CGRectMake((320-210)/2, 0, 210, 50)];
customTitleView.backgroundColor = [UIColor clearColor];
//create your UITextField or UILabel or other view and add as subview of customTitleView
self.navigationItem.titleView = customTitleView;

Custom NSButton with semi-transparent background

I'm trying to create a custom NSButton with a 50% opaque black background and white text. To do this I've subclassed NSButton and overloaded DrawRect:
- (void) drawRect:(NSRect)dirtyRect
{
[self setBordered:NO];
//REMED since it has same effect as NSRectFill below
//[[self cell] setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.2]];
NSColor* backgroundColor = [NSColor colorWithCalibratedWhite:0 alpha:0.3f];
[backgroundColor setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
The white text appears fine but the button's background is always 100% opaque. The alpha value is not interpreted.
Any ideas? Thanks!
The default operation of NSRectFill() is copy which is not what you want. Replace it with
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceAtop);
Another solution I found was to keep my code the same but turn on the Core Animation Layer for each button in Interface Builder. I don't know enough about Core Animation Layer to know why this worked. I had previously turned CAL off because it was making my fonts look very jagged.

How To Draw A Line Dividing The Cell's Accessory View

I just wanted to know how I could draw a line dividing the cell's accessory view from the rest of the cell.
You could simply add a thin UIView (or UIImageView if you wanted a fancier divider) like so.
UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(290, 3, 1, 38)];
[divider setBackgroundColor:[UIColor blackColor]];
[cell.contentView addSubview:divider];
[divider release];
If you're using one of UITableView's default accessory views, your content view will be squashed to make room for the accessory, so you your x-coordinate may have to change to sit at the very edge of the content view.

Custom NSView Fill Paints Over Bottom Bar

I have a window which has a custom NSView and has a bottom bar with controls on it, one of which is an NSColorWheel.
For simplicity sake the Window is 332px high, with the custom NSView being 300px high and the bottom bar being 32px high.
The bottom bar is created as part of my awakeFromNib when the app loads the window using the following code:
[[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
[[self window] setContentBorderThickness: 32.0 forEdge: NSMinYEdge];
In my custom NSView class I fill the rectangle with color. Everything works fine when the app loads using the following in my NSView class:
- (void)drawRect:(NSRect)dirtyRect
{
dirtyRect = [self bounds];
NSColor * mNewColor = [NSColor blackColor];
[mNewColor set];
[NSBezierPath fillRect:dirtyRect];
}
However, if I subsequently call a method that changes the color of the custom NSView when a color wheel in the bottom bar is changed, the bottom bar gets overwritten with the color. The following code illustrates this method (this code is in the custom NSView class:
- (void)changeBackgroundColor:(NSNotification *)notification
{
NSLog(#"Changed background color");
NSRect mRect = [self bounds];
NSColor * mNewColor = [theColorWell color];
[mNewColor set];
[NSBezierPath fillRect:mRect];
[self setNeedsDisplay:YES];
}
Resizing the window instantly corrects the problem, but obviously I don't want the user to have to resize the window for an obvious bug!
What I don't understand is why my bounds appear to be mapping to the parent window and not the custom NSView when I call setNeedsDisplay and yet the bound correctly adjust when I resize the window using the mouse (even if just by 1 pixel).
Do I somehow need to account for the bottom bar on the redraw?
Any and all help much appreciated.
You should do all your drawing in the drawRect: method of your custom NSView. Cocoa automatically sets up the graphics context for you when it calls this method - things may not draw correctly if you perform drawing operations in other methods.
Your code in drawRect: could set the colour to the the current background colour as specified by your NSColorWell and fill the dirtyRect rectangle with this.
Then in the other method just call [self setNeedsDisplay:YES]; and then drawRect: will automatically be called to redraw the view.
See here for more information: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW4 (in particular the Drawing View Content section)