Re: Placing an Image next to a UI element
Re: Placing an Image next to a UI element
- Subject: Re: Placing an Image next to a UI element
- From: David Mirabito <email@hidden>
- Date: Sat, 26 Nov 2011 11:44:28 +0000
Thanks for the tips Fritz,
To address a couple of your points:
* Yes, my 'make the image square' method is a bit of a hack. It really should be a fixed size and centred with respect to the textbox I'm drawing attention to. That was stage 2 after getting it positioned correctly :)
* Thanks for the reminder to use the string symbol name. Using the string literal was a hold over from when I was using my own resource image. IB doesn't show NSInvalidFreestandingTemplate in the drop down list of available images so I only found it later.
* I took your advice and hoisted the parent view into a new subclass, and simply overrode drawRect:
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
for (NSView *v in [self subviews]) {
NSRect oldframe = [v frame];
NSRect newFrame = NSMakeRect(
oldframe.origin.x + oldframe.size.width + 26,
oldframe.origin.y,
oldframe.size.height,
oldframe.size.height);
[[NSColor redColor] set];
NSRectFill(newFrame);
[[NSColor blueColor] set];
NSRectFill(oldframe);
}
}
This behaves exactly as I expect - the labels/popupmenu are slightly framed in blue. (except NSTextField which apparently draws 100% of its own frame) and a red box is in the position where I want the alert icon to be. (and yes you are right, the 26/20 points-squares are appear obviously different and distracting! :) )
So in retrospect - I think it makes sense: I was adding the subviews quite early on - effectively within NSDocument's dataOfType:error method. At this time my view is still the same size as it is in the NIB - and hasn't yet been stretched to fit in it's superview. Cocoa must change the frame positions when a subview is 'anchored' to the top of it's parent, the coordinates' origin is at the bottom, and the superview's size changes.
So I see I have two options:
a) draw the alert later (perhaps even in an override of drawRect;)
b) figure out how to programaticially set the edge of the superview to 'anchor' the new image to. (like how you can graphically select in IB)
So I think I'm good for now, thanks for the tips that showed me the light!
-DavidM
On 25/11/2011, at 5:41 PM, Fritz Anderson wrote:
> On 25 Nov 2011, at 7:45 AM, David Mirabito wrote:
>
>> NSView *aview = theOffendingNSTextView;
>
> Is this in fact an NSTextView, or do you mean NSTextField?
>
>> NSView *superView = [aview superview];
>>
>> NSRect oldframe = [aview frame];
>>
>> NSRect newFrame = NSMakeRect(
>> oldframe.origin.x + oldframe.size.width + 26,
>> oldframe.origin.y,
>> oldframe.size.height,
>> oldframe.size.height);
>
> So even if the view being annotated is 300 pt tall, and the icon image is 32 pt tall, you're going to make the icon view 300 x 300? And if the origin of the annotated view is 24 pt (remember that's measured _from the bottom_, you're going to align the annotation at the bottom of the annotated view? If your text views are the only ones you've tried that are taller than the others you've annotated, the annotation view will ride lower than you expect.
>
> Also, many view frames differ in size and origin from the visually-apparent frames. To diagnose, try subclassing your enclosing view (if you haven't already), and in drawRect: iterate through the subviews and draw rectangles around their frames. A very cursory skim of the class references don't show me anything that would help you get the layout rects, but maybe you can hold your nose and derive the layout rects from the frames empirically.
>
> Maybe the 10.7 autolayout methods could help you; it may be good enough to align your annotations with the annotated views' baselines.
>
>> NSImageView *img = [[NSImageView alloc] initWithFrame:newFrame];
>> [img setImage:[NSImage imageNamed:@"NSInvalidDataFreestandingTemplate"]];
>
> This isn't your problem, but never use literal strings for Cocoa's exported global strings. You don't know whether the constant value is @"com.apple.aStr", and the symbol they published to the API is NSViewFrobbingA.
>
>> [superView addSubview:img];
>
> — F
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden