Minimize / miniaturize cocoa NSWindow without titlebar - objective-c

I'm stuck! Obviously... because I'm posting a question here.
I have built my own custom window controls for my OS X / cocoa app. The close button works great -- no problems. The minimize button doesn't work at all when I disable the titlebar.
So when the title bar is on like the image above and I hit this method, the minimizing works fine:
ViewController.h
#interface ViewController : NSViewController {
- (IBAction)minimize:(id)sender;
#property (strong) IBOutlet NSButton *btn_minimize;
}
#end
ViewController.m
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)minimize:(id)sender {
[[NSApp mainWindow] performMiniaturize:self];
}
-(IBAction)terminate:(id)sender {
[NSApp terminate:self];
}
#end
Then if I disable the title bar, that same method stops working. No errors, nothing. I've tried both: [[NSApp mainWindow] miniaturize:nil]; and also [[NSApp mainWindow] performMiniaturize:self];. Neither of which worked. Actually... the both work IF the title bar is on. But once I turn it off, neither work.
Thoughts / comments?
Oh, I'm using Storyboards, Xcode 7, and am targeting 10.10 and using the 10.11 SDK if that matters at all.
Thanks in advance.

You have to keep the original "traffic light" buttons around and hide them manually.
Here's how I've configured the window:
self.titleVisibility = NSWindowTitleHidden;
self.titlebarAppearsTransparent = YES;
self.movableByWindowBackground = YES;
And here's how I've hidden the traffic lights:
for (id subview in self.contentView.superview.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"NSTitlebarContainerView")]) {
NSView *titlebarView = [subview subviews][0];
for (id button in titlebarView.subviews) {
if ([button isKindOfClass:[NSButton class]]) {
[button setHidden:YES];
}
}
}
}
See sample project here.

In my case, I wanted to completely remove the title bar and trigger the miniaturization through some other means (eg: a key assignment).
I found that ensuring that the window style mask includes NSMiniaturizableWindowMask was required in order for NSWindow::miniaturize to have an effect.

Adding to Wez Furlong's answer, this is how to do it in Swift 5.
Assuming you have an NSWindow named "theWindow", do the following:
theWindow.styleMask = theWindow.styleMask.union(.miniaturizable)
Basically, you perform a set union of the window's existing style mask with the .miniaturizable option.

Related

How do you specify the origin of the arrow on a popover segue with OS X 10.10 storyboards

I'm playing around with storyboarding in an OS X 10.10 app. I have an NSTableView that, when you click a specific row opens a segue that goes to a popover that contains an NSViewController.
How do you specify the origin NSPoint of the arrow for the popover? Right now, it just points to the NSTableView in the middle. I assumed that I could do this in prepareForSegue, but I can't seem to figure it out. prepareForSegue doesn't seem to have an understanding that the NSViewController is contained in an NSPopover
Any ideas?
You should file an enhancement request Radar for this behavior if you think it should be provided by the framework in some way.
But to workaround this in the meantime, you can create your own custom NSStoryboardSegue subclass to help with this.
#interface TablePopoverSegue : NSStoryboardSegue
#property (weak) NSTableView *anchorTableView;
#property NSRectEdge preferredEdge;
#property NSPopoverBehavior popoverBehavior;
#end
#implementation TablePopoverSegue
- (void)perform {
if ([self anchorTableView]) {
NSInteger selectedColumn = [[self anchorTableView] selectedColumn];
NSInteger selectedRow = [[self anchorTableView] selectedRow];
// If we can pick a specific row to show from, do that; otherwise just fallback to showing from the tableView
NSView *anchorView = [self anchorTableView];
if (selectedRow >= 0) {
anchorView = [[self anchorTableView] viewAtColumn:selectedColumn row:selectedRow makeIfNecessary:NO];
}
// Use the presentation API so that the popover can be dismissed using -dismissController:.
[[self sourceController] presentViewController:[self destinationController] asPopoverRelativeToRect:[anchorView bounds] ofView:anchorView preferredEdge:[self preferredEdge] behavior:[self popoverBehavior]];
}
}
#end
This can be specified in IB in the inspector panel for the segue (just like iOS):
And then in your source view controller's prepareForSegue:, you can just set up the segue:
- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
if ([segue isKindOfClass:[TablePopoverSegue class]]) {
TablePopoverSegue *popoverSegue = (TablePopoverSegue *)segue;
popoverSegue.preferredEdge = NSMaxXEdge;
popoverSegue.popoverBehavior = NSPopoverBehaviorTransient;
popoverSegue.anchorTableView = [self tableView];
}
}

Custom NSToolbarItem Button Not Showing

I have two custom NSToolbarItems in the toolbar of the application. Each class has a NSButton within, where I setup the button and then set the toolbar item's view to the button (the stop button item for example):
#implementation RBSStopButtonToolbarItem
#synthesize button = _button;
-(id)initWithItemIdentifier:(NSString *)itemIdentifier
{
self = [super initWithItemIdentifier:itemIdentifier];
if(self)
{
// create button
_button = [[NSButton alloc] init];
// set the frame and bounds to be the same size
//[_button setFrameSize:NSMakeSize(64.0, 64.0)];
//[_button setBoundsSize:NSMakeSize(64.0, 64.0)];
// button will not have a visible border
[_button setBordered:NO];
// set the original and alternate images...names are "opposite"
[_button setImage:[NSImage imageNamed:#"StopButtonAlternateIcon"]];
[_button setAlternateImage:[NSImage imageNamed:#"StopButtonIcon"]];
// image position
[_button setImagePosition:NSImageOnly];
// set button type
[_button setButtonType:NSMomentaryChangeButton];
// button is transparent
[_button setTransparent:YES];
// set the toolbar item view to the button
[self setView:_button];
}
return self;
}
I have an IBOutlet for each custom NSToolbarItem:
// toolbar item for start button
IBOutlet RBSStartButtonToolbarItem *_startButtonToolbarItem;
// toolbar item for stop button
IBOutlet RBSStopButtonToolbarItem *_stopButtonToolbarItem;
Yet I do not see the images in the custom view toolbar items:
The images are .icns type. The example I attempted to following is here:
NSButton in NSToolbar item: click issue
Is there anyone with experience who can offer advice?
I don't know why, but:
[NSToolbarItem initWithCoder:] is calling [NSToolbarItem setImage:] which is then calling [NSButton setImage:] on the button you have set as the toolbar item's view. This wipes out what you have done.
The example that you are referring to DOES NOT subclass NSToolbarItem.
I recommend that you also DO NOT subclass NSToolbarItem, and instead add a regular NSToolbarItem to the toolbar via interface builder and then in awakeFromNib find that toolbar item via its item identifier and set the button as its view.
I have verified that doing it this way works as expected.
I do not follow why your example doesn't work.
But I have worked out the custom NSToolbarItem with my own way without even using NSToolbarDelegate.
My way is assuming you build your toolbar within a nib and not with code(mostly).
What I am doing is creating my own NSView in my nib with whatever I want in it.
Then I drag this NSView into into my NSToolbar in my nib.
xCode will automatically place your NSView inside an NSToolbarItem.
You can then drag this custom NSToolbarItem into the default items and place it with whatever order you want(so you don't even need to place it by code).
The tricky part is to subclass NSToolbarItem and then within the awakeFromNib of this specific NSToolbarItem subclss you set it's view to the NSView underneath it.
You would also need to refer the NSView into an IBOutlet * NSView within that subclass.
Here is the code of the subclass.
The header file:
#import <Cocoa/Cocoa.h>
#interface CustomToolbarItem : NSToolbarItem
{
IBOutlet NSView * customView;
}
#end
The Obj-c file:
#import "CustomToolbarItem.h"
#implementation CustomToolbarItem
-(instancetype)initWithItemIdentifier:(NSString *)itemIdentifier
{
self = [super initWithItemIdentifier:itemIdentifier];
if (self)
{
}
return self;
}
-(void)awakeFromNib
{
[self setView:customView];
}
#end
I have also wrote a blog post about how I did this:
http://pompidev.net/2016/02/24/make-a-custom-nstoolbar-item-in-xcodes-interface-builder/

Can't get UITouch event from UIButton

Problem:
I can't get my UIButton to send my Class an Event call
Here is what I tried first:
I made a class which extends UIViewController, called Viewer
#import "Viewer.h"
#import "MainScreen.h"
#implementation Viewer
- (void)viewDidLoad
{
MainScreen* main = [[MainScreen alloc]init: self];
[main show];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
then:
I created a class called MainScreen, 2 IBOutlets - singlePlayerButton and view
Viewer is an instance of UIViewController
buttonPressed has the return type of IBAction
MainScreen:
#import "MainScreen.h"
#import "Viewer.h"
#implementation MainScreen
{
IBOutlet UIButton* singlePlayerButton;
IBOutlet UIView* view;
Viewer* viewer;
}
- (id)init:(Viewer*) theViewer
{
self = [super init];
viewer = theViewer;
return self;
}
- (void)show
{
[[NSBundle mainBundle] loadNibNamed:#"MainScreenLayout" owner:self options:nil];
[viewer.view addSubview:view];
}
- (IBAction)buttonPressed:(id)sender
{
NSLog(#"A button was pressed!");
}
#end
Then I created an NIB called MainScreenLayout.xib
Here is what it looks like:
As you can see, I have four buttons but the only one that is connected to anything is the Single-Player button I also have the UIView connected to view.
Now, I linked the action to the Single-Player button like this:
buttonPressed is my method that I want to have called when the button is pressed, so I ctrl-clicked on First Responder and then clicked on the circle next to buttonPressed and then dragged it on top of the Single-Player button. Then the next dialog window popped up and I clicked on Touch up Inside. Here is what the Received Actions window looks like:
Now, I thought at this point it should work. I ran it on my iPhone Simulator and pressed the button.
Just so there is no question of whither or not I'm competent, here is a picture of me actually clicking the button:
I got no output whatsoever. Then I went to my code and looked at the method. This is what it looks like:
as you can see, the oval is not filled, meaning that the method is not paired. So naturally I thought it was being overridden by another method. So here is what the method looks like now:
- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender
{
NSLog(#"A button was pressed!");
}
Then I went back to my NIB window, unpaired my Single-Player button and then repaired it to aVerySpecificMethodNameForMyButtonListener. Now this is what it looks like:
Then I ran it again and still nothing when I pressed the button. Then I went back to the code:
(insert frustration here)
At this point I restarted Xcode and the Simulator and looked over everything again and it still didn't work.
This is what I tried next which seems about a billion times more simple:
- (void)show
{
[[NSBundle mainBundle] loadNibNamed:#"MainScreenLayout" owner:self options:nil];
[viewer.view addSubview:view];
[singlePlayerButton addTarget:self action:#selector(aVerySpecificMethodNameForMyButtonListener:) forControlEvents:UIControlEventTouchUpInside];
}
I replaced with my show method with the one above. The only thing different is the last line. I disconnected my method from the Single-Player button in the NIB. Then I ran it and I got excited for a second because I got some output. Here is what I got:
(lldb)
Very helpful output C debugger, as always. I spent the next few hours on SO trying to find out what I'm doing wrong. I've tried countless examples and none of them worked.
Does anybody see what I'm doing wrong here? Is it just Xcode being flakey?
Thanks for reading!! All relevant answers are welcomed!
UPDATE:
I also tried changing the declaration of UIButton to a property in the Header file:
#import <Foundation/Foundation.h>
#import "Viewer.h"
#interface MainScreen : NSObject
{
IBOutlet UIView* view;
Viewer* viewer;
}
#property (weak, nonatomic) IBOutlet UIButton* singlePlayerButton;
- (id)init:(Viewer*) theViewer;
- (void)show;
- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender;
#end
Thus here is what I changed in MainScreen.m to comply:
#import "MainScreen.h"
#import "Viewer.h"
#implementation MainScreen
- (id)init:(Viewer*) theViewer
{
self = [super init];
viewer = theViewer;
return self;
}
#synthesize singlePlayerButton;
- (void)show
{
[[NSBundle mainBundle] loadNibNamed:#"MainScreenLayout" owner:self options:nil];
[viewer.view addSubview:view];
[singlePlayerButton addTarget:self action:#selector(aVerySpecificMethodNameForMyButtonListener:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender
{
NSLog(#"A button was pressed!");
}
#end
Now there are no variable declarations inside of the MainScreen.m file, I moved them all to the Header. The circles next to singlePlayerButton and view are still filled in but the bubble next to the method is not. I ran the code again and got the same output.
So I'd have put this in a comment because its really just a suggestion however I dont have the rep yet so sorry to get your hopes up, but I believe the touch event's scope is only inside the UIView in which it happens. When the touch up event gets triggered it doesn't get passed to the UIView's handler (MainScreen class) and so the event's listener method does not get called... but that you are able to link it in the interface builder to the button is baffling to me, so i could be way off.
Anyways, where I think you should go next is to make your MainScreen a UIView object and see what that does for event handling of the buttons in the view.

UIWebview remove tap to focus

A UIWebView is normally focused on the selected content when I double tap a part of a website, and now I want the UIWebView to ignore the double tap but still be able to be interacted with.
How would I go about this?
The Double Tap is recognized by a UITapGestureRecognizer.
Go through the view hierarchy if you really want this.
A little bit tricky, but it should work.
The codes look like this:
- (void)goThroughSubViewFrom:(UIView *)view {
for (UIView *v in [view subviews])
{
if (v != view)
{
[self goThroughSubViewFrom:v];
}
}
for (UIGestureRecognizer *reco in [view gestureRecognizers])
{
if ([reco isKindOfClass:[UITapGestureRecognizer class]])
{
if ([(UITapGestureRecognizer *)reco numberOfTapsRequired] == 2)
{
[view removeGestureRecognizer:reco];
}
}
}
}
I've put a demo here: NoDoubleTapWebView
Hope it helps.
iOS 5.0+:
A UIWebView contains a UIScrollView to display its content and is accessible by the scrollView property of UIWebView. So, one possible way of doing this is to subclass UIScrollView and override -touchesShouldBegin:withEvent:inContentView. You can get the number of taps from a UITouch object from its tapCount property, which can be used to filter out double-tap gestures and call the super method otherwise. This would look something like:
-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
UITouch * touch = [touches anyObject]; //Get a touch object
if (2 == touch.tapCount) { //If this is a double-tap...
return NO; //...don't begin touches
}
else { //Otherwise, call the superclass' implementation
return YES;
}
}
However, in order to set the web view's scroll view as your custom subclass, you may also have to subclass UIWebView (which is generally frowned upon). With that said, it would likely work to subclass UIWebView and redefine the scrollView property. In the interface file:
#interface MyAwesomeWebView : UIWebView {
}
#property (nonatomic, readonly, retain) MyAwesomeScrollView * scrollView;
#end
And in the implementation file:
#implementation
#dynamic scrollView
#end
Still, proceed with caution.
Pre iOS 5.0:
As Alan Moore wrote in the comments, you can add a transparent view on top of the web view, capture all touch events with hit testing, and forward the touch events to the web view unless there's a double tap.
EDIT: Updated answer to include caveats about subclassing UIWebView, etc.
EDIT 2: Included second answer for pre iOS 5.0
I think the way to do it is to add a UITapGestureRecognizer on top of your UIWebView, then you can filter double taps as you wish.
You can find sample code/discussion here:
Add a UITapGestureRecognizer to a UIWebView

firstResponder in NSViewController

I've got two classes. ManagingViewController, a subclass of NSViewController, and ViewController, a subclass auf ManagingViewController. In Viewcontroller I've got a NSTextField which I want to become the firstResponder, but I didn't manage that.
So it is nearly the same like the Chapter 29 in Hillegass' book Cocoa Programming for Mac OS X (Download of the book's examples) except of an NSTextField which is set to firstResponder.
Can anybody point me to the correct way?
You need to set the text field as the first responder by using -[NSWindow makeFirstResponder:].
Since this is an NSWindow method, it only makes sense after you’ve added the corresponding view to the window, i.e., after you’ve added the view as a subview inside the window view hierarchy. In the book’s example, this happens when you set the view as the content view of the box inside the window. For example:
- (void)displayViewController:(ManagingViewController *vc) {
// Try to end editing
NSWindow *w = [box window];
…
// Put the view in the box
NSView *v = [vc view];
[box setContentView:v];
// Set the first responder
if ([vc class] == [ViewController class]) {
[w makeFirstResponder:[(ViewController *)vc myTextField]];
}
}
This assumes ViewController exposes a getter method called -myTextField.
You can make this more generic by having your view controllers expose a method that returns the object that the view controller recommends as the first responder. Something like:
#interface ManagingViewController : NSViewController
…
- (NSResponder *)recommendedFirstResponder;
#end
#implementation ManagingViewController
…
- (NSResponder *)recommendedFirstResponder { return nil; }
#end
And, in your concrete subclasses of ManagingViewController, have -recommendedFirstResponder return the object that should be the window’s first responder:
#implementation ViewController
…
- (NSResponder *)recommendedFirstResponder { return myTextField; }
#end
Having done that, you can change your -displayViewController: to something like:
- (void)displayViewController:(ManagingViewController *vc) {
// Try to end editing
NSWindow *w = [box window];
…
// Put the view in the box
NSView *v = [vc view];
[box setContentView:v];
// Set the first responder
NSResponder *recommendedResponder = [vc recommendedFirstResponder];
if (recommendedResponder) [w makeFirstResponder:recommendedResponder];
}
Have you tried [[myTextField window] makeFirstResponder:myTextField]; ?
simple. Goto you xib file in interface builder. right click the first responder field. it will show the connection , remove the connection and connect it to the desired responder. let me know if this works