How to keep NSPanel within NSScreen visibleframe always - objective-c

I have one floating NSPanel in my app, which user can move using mouse. I want my NSPanel to always visible within main screen. I want code to reposition my NSPanel with its original width and height within screen border in all the sides.
Thanks,

Subclass NSPanel and override the frame-relative methods. setFrameOrigin:, setFrame:display: and maybe others (see the documentation to find out all the methods). There is also promising - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen method.

Related

Change NSButton title position

I want to change the title position of NSButton that has a background image so that i can place the title in the bottom left corner of the button and in the same time overlaps the image
Sounds like you need to override the drawing methods in NSButtonCell (that's what NSButton uses for drawing). I've created subclasses that "fake out" the NSButtonCell implementation for simple adjustments. To do this, create a subclass of NSButtonCell and set it up as the cell for your NSButton. Next override the drawing methods of NSButtonCell (there are multiple depending on what you're trying to do), adjust your target frame and then call the superclass (NSButtonCell) implementation.
Depending on how much you need to do though, you may just be writing your own drawing methods. This gives you the most control.

How to create a custom NSSlider with a transparent middle bar and custom knob size

I've searched around for some code on NSSliderCell but I can't find a way to do what I'm looking for.
I'd like to create something like this (where the white line is the slider knob, 1 pixel width):
I'm going to use this for the time bar of a music track, so it's going to move every second (that's why I want to use a NSSlider to make things easy).
What do I need to do to make a slider, with a transparent middle bar, similar to the image above?
PS: It's not going to be touchable, it's just for display.
You can just override drawRect:, as when subclassing NSView, except in your implementation, use [self doubleValue] to determine where to draw the line. However, I don't see the point of subclassing NSSlider in this case. A custom NSView could have a property that determines where to draw the line, and then other code could set that property or bind to it.
That looks like a vertical split view to me with a 1 pixel wide divider. You might try that. There's a method to set the position of the divider so it would be easy to move as you need. And you can make the divider 1 pixel by creating a subclass of NSSplitview and overriding the dividerThickness method to return 1. Then you just set the background of the 2 subviews to black and there you go. It's easy to try so maybe it will work for you. Good luck.
I finally got it:
I created a NSSliderCell subclass with a property #property float x;
I overrode the drawKnob method and inside it I wrote:
-(void)drawKnob:(NSRect)knobRect{ self.x = knobRect.origin.x; }
I dragged a NSSlider into my window (made it small, changed it's width to the window's width) and changed it's cell class to the one I created;
And then when the music is playing, every time a second goes by I do:
[_timeBarSlider setMinValue:0];
[_timeBarSlider setMaxValue:myTrack.duration];
[_timeBarSlider setDoubleValue:myPlayer.currentPosition];
[[_timeBarImageView animator] setFrame:NSMakeRect(_timeBarSliderCell.x, yourYCoordinate, yourWidth, yourHeight)];
_timerBarSlider is the NSSlider I have in IB / _timerBarImageView is the image view that contains the vertical image line / _timerBarSlderCell is the NSSlider's cell (subclassed)
PS: the NSSlider is behind every object in that window, so that the user can't see it. You can't setHidden:YES on it because the drawKnob method will not be called.

UIPopoverController's UIView: How to remove shadow?

Following is a snapshot of my popover:
I have an UIViewController (say A) that I want to show as popoverController. Also I want to customize popover so I am customizing the UIPopoverBackgroundView (say 'b' - orange background). Everything is working right except that after rounding the corners of A.view and 'b' I have a rectangular shadow line (which from the snapshot probably belongs to A.view - green background). Based on UIView or UIViewController APIs, is there any way I can remove that shadow. I am only interested in removing the black line on left, top and right of the view not the fuzzy looking shadow.
I did try:
[A.view.layer setShadowOpacity:0.0];
but no luck.
In your UIPopoverBackgroundView subclass, override the class method:
+ (BOOL)wantsDefaultContentAppearance
To return NO. This prevents the drawing of the inner shadow as documented here.
Remove the call for [super layoutSubviews] in your layoutSubviews method.
Overriding wantsDefaultContentAppearance and returning NO did not work for me.
You cannot control the shadow of a UIPopoverController directly. Your options are:
Roll your own kind of popover.
Subclass UIPopoverBackgroundView and provide your own graphics for the frame of the popover. Contrary to the official documentation, the shadow doesn't get drawn on for you when you subclass UIPopoverBackgroundView. Here's a great post on how to do this: http://blog.andrewkolesnikov.com/custom-background-color-tint-for-uipopover-64835

NSProgressIndicator in NSTableCellView

I have an NSTableCellView subclass that contains an NSProgressIndicator.
I have it all updating and animating properly, however it remains greyed out. I suspect this is a view loop issue.
Any thoughts?
NSProgressIndicator will only appear blue if it's on the key window.
If your window isn't the key window you can send it that message [myWindow makeKeyWindow]
Window's without title's or resize bars may not respond to that message, so you may need to subclass your window and override
- (BOOL)canBecomeKeyWindow{return YES}.
This was a problem I ran into with the tint color of an nsprogressindicator on a sheet.
The doc's say to never invoke this method directly.. [myTitlelessWindow becomeKeyWindow] but you can use it to confirm the problem before subclassing your window.

How can I remove the resize corner from an NSPanel that is an HUD Window

I'm trying to remove the resize corner from an NSPanel HUD styles.
This is what I'm trying right now.
NSUInteger currentStyleMask = [somePanel styleMask];
[somePanel setStyleMask: currentStyleMask | !NSResizableWindowMask];
I also get a warning that NSWindow may not respond to setStyleMask and it isn't defined in NSWindow.h, however it is defined in the NSWindow documentation.
You can only set the style mask of an NSWindow at creation time, you can't change it once the window has been instantiated.
You need to create the window programmatically and initialize it by calling -initWithContentRect:styleMask:backing:defer: and pass in your desired style mask.
If you want to manipulate the window in Interface Builder instead of creating it programmatically, you'll need to subclass NSWindow, override -initWithContentRect:styleMask:backing:defer: and pass in your style mask to super's implementation. You can then assign your subclass to the window in Interface Builder.