I am writing a Mac app with minimal IB usage -- see programatically create initial window of cocoa app (OS X).
Here is my code (via Xamarin/C#):
public override void DidFinishLaunching(NSNotification notification) {
// create and show a window, then . . .
NSMenu fileMenu = new NSMenu();
NSMenu appMenu = new NSMenu();
// add some items to both menus, then . . .
NSMenuItem file = new NSMenuItem("file");
file.Submenu = fileMenu;
NSMenuItem app = new NSMenuItem("app");
app.Submenu = appMenu;
NSMenu topLevelMenu = new NSMenu();
topLevelMenu.AddItem(app); // these two lines in
topLevelMenu.AddItem(file); // either order
NSApplication.SharedApplication.Menu = topLevelMenu; // incidentally, setting the MainMenu doesn't appear to do anything
}
}
The weird thing is that only the first menu item I add shows up, and its title is changed to the name of my app. So for the above code, I would see the app menu, only, with a title of MyAppName, but the correct items. If I were to instead add the file NSMenuItem first, I would see that menu, again with its name changed to my app name, and the view menu would not show up at all.
I don't mind the changed title; I suppose Apple wants to always show the name of the active app. But it bugs me that I can't get the additional menus to appear.
How can I get all of my menus to show up across the top of the screen?
I just needed to add titles to my menus:
fileMenu.Title = "file";
appMenu.Title = "whatever";
The title of the first one is still changed to the name of the app.
Related
I'm quite new in swift programming but couldn't find an answer to the question how to change the title of the navigation bar when the user taps on it (see example app "ToDo" from Microsoft [former Wunderlist]). So first I choose the "Item A" out of a tableview and in the following screen the title stands in the navigation bar - so far so good. With a tap on this title "Item A" it should be possible to change the title (also for the previous tableview with the list of items by saving the changes in Realm). For now I just use the commands
title = selectedItem!.title
in my viewDidLoad in the upcoming view controller where .title equals the title from my "Item"-class
class Item: Object {
var parentCatergory = LinkingObjects(fromType: Category.self, property: "items")
#objc dynamic var title: String = ""
}
Any ideas? Or do I have to use a customized navigation bar, if so how?
Thanks in advance!
According to the UIViewController lifecycle, anything that you add to the method viewDidLoad() will be executed once when the view is loaded. If you want to change the title property on demand on a ViewController, is necessary invoke the navigation bar title property again from your tab action method. Check this post for more information: How to set the title of a Navigation Bar programmatically?
Is there a way to tell OS X to automatically style/tint a NSToolbarItem?
I've added an "Image Toolbar Item" via IB/Xcode and set the icon to a black PDF as described in the documentation.
However, my result does not resemble that of, for instance, the App Store:
I'm looking for something akin to what the TabBar in iOS does by default.
I'm new to OS X development... So any guidance would be appriciated!
Images need to be made template'd in order to get the correct styling (such as the engraved & blue styling).
This can be done in code with -[NSImage setTemplate:] or by having your image names end with "Template" (requiring no code changes).
To get the blue styling specifically, you have to set a borderless NSButton as the custom view of the toolbar item (rather than it being a standard item). That button has to have a type that results in it showing its state (e.g. a Round-Textured Toggle button), and when it has an On state, it will get then blue styling.
If you're trying to create a tinted toolbar item in code, This is how I did it. Create the correct type of button NSButtonTypeToggle then set the buttons properties, then add the button to the toolbar item's custom view and finally the toolbar item is returned.
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
// create toolbar items
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
toolbarItem.label = #"title";
NSImage *iconImage = [NSImage imageNamed:NSImageNameColumnViewTemplate];
NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 40.0, 40.0)];
button.title = #"";
button.image = iconImage;
[button setButtonType:NSButtonTypeToggle];
button.bezelStyle = NSBezelStyleTexturedRounded;
button.action = #selector(toggleColumnView:);
[toolbarItem setView:button];
return toolbarItem;
}
I am trying to create a localized windows phone 8 app , and I am creating a global application bar . However I am unable to set the text of the items inside the bar using the resource , anyone have an idea how to do that ? I am also unable to get it in code .
you can reuse the commented code in the template's MainPage.xaml.cs:
private void BuildLocalizedApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
// Create a new button and set the text value to the localized string from AppResources.
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
//Search in AppResources.Search is a key of a string in the resource files (.resx)
appBarButton.Text = AppResources.Search;
ApplicationBar.Buttons.Add(appBarButton);
// Create a new menu item with the localized string from AppResources.
//Search in AppResources.Search is a key of a string in the resource files (.resx)
ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.Search);
ApplicationBar.MenuItems.Add(appBarMenuItem);
}
unfortunately the app bar is not a traditional Silverlight control so you have to create programmatically in every page if you want it localized.
In my OS X app, using Interface Builder, I have a window that looks like this:
I'd like to add a button to the right-hand side, to achieve this:
If this is possible, how can I do it?
It is not possible to do with Interface Builder, however you can get it done with little bit of coding :
NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton]; // Get the existing close button of the window. Check documentation for the other window buttons.
NSView *titleBarView = closeButton.superview; // Get the view that encloses that standard window buttons.
NSButton *myButton = …; // Create custom button to be added to the title bar.
myButton.frame = …; // Set the appropriate frame for your button. Use titleBarView.bounds to determine the bounding rect of the view that encloses the standard window buttons.
[titleBarView addSubview:myButton]; // Add the custom button to the title bar.
Swift 2.2 and Auto Layout, Create an "OK" button to the right of the title bar:
let myButton = NSButton()
myButton.title = "OK"
myButton.bezelStyle = .RoundedBezelStyle
let titleBarView = window!.standardWindowButton(.CloseButton)!.superview!
titleBarView.addSubview(myButton)
myButton.translatesAutoresizingMaskIntoConstraints = false
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[myButton]-2-|", options: [], metrics: nil, views: ["myButton": myButton]))
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-1-[myButton]-3-|", options: [], metrics: nil, views: ["myButton": myButton]))
With auto layout, you don't need to hard-code button's frame. And it is always on the right of the title bar even you resize the window.
I need to remove some items from right-click context menu, but the toolbar does not contain a public API to access 'toolbarView'.
Is it possible to customize the menu without using private API?
You can access and modify a toolbar contextual menu when the toolbar is created, i.e. in -[awakeFromNib]:
- (NSMenu *)toolbarMenuInWindow:(NSWindow *)window
{
NSView *contentView = window.contentView;
NSView *toolbarView = contentView.superview.subviews.lastObject;
NSMenu *toolbarMenu = toolbarView.menu;
return toolbarMenu;
}
Now you can directly edit menu items and hide or disable them.