How do I disable the Show Tab Bar menu option in Sierra apps? - nswindow

I've got an app that uses a Toolbar in a NSWindow. I don't want users to be able to customize this toolbar for aesthetic reasons. In Sierra there's a new Menu option that gets inserted into "Menu > View" called Show Tab Bar. How do I disable this? Enabling it only seems to increase the tool bar's height as I don't have extra labels showing under the icons.

You can also do this on IB, on the Window’s attributes inspector

On 10.12, you need to now set the following when the window is created as Tab Bar is now available by default:
[NSWindow setAllowsAutomaticWindowTabbing: NO];
The answer is the same in Swift and SwiftUI
func applicationWillFinishLaunching(_ notification: Notification) {
NSWindow.allowsAutomaticWindowTabbing = false
}
Note that the call is made on the class NSWindow not on an instance of NSWindow

To disable tabbing on individual windows call setTabbingMode:
if([window respondsToSelector:#selector(setTabbingMode:)]) {
// this particular window doesn't support tabbing in Sierra.
[window setTabbingMode:NSWindowTabbingModeDisallowed];
}

If you don't want to compile against the latest frameworks, you can use the following code in your NSWindowsController sub classes:
Swift:
override func awakeFromNib() {
if NSAppKitVersionNumber > 1500 {
self.window?.setValue(2, forKey: "tabbingMode")
}
}
Objective-C:
- (void)awakeFromNib {
if (NSAppKitVersionNumber > 1500) {
[self.window setValue:[NSNumber numberWithInt:2] forKey:#"tabbingMode"];
}
}

Swift solution:
override func awakeFromNib() {
super.awakeFromNib()
if #available(OSX 10.12, *) {
tabbingMode = .disallowed
}
}

Swift 5
In your NSWindowController:
self.window?.tabbingMode = .disallowed

Related

Swift iOS 13 - scopeBar in searchBar not displayed in TitleView

Prior to iOS 13, when the search bar became the first responder, the below code displayed the scope bar with two scope bar buttons, which is the intended behavior.
With iOS 13, the search bar still functions correctly, however the scope bar is no longer being displayed. Apple's documentation for the UISearchBarDelegate seems to suggest using .setShowsScope(show:(bool), animated:(bool)) and .sizeToFit() for iOS 13. However, these function calls seemed not to have any effect. Looking for ideas to have the scope bar displayed as it did with iOS 12 and earlier.
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
//eventSearchBar is name of UISearchBar
//eventSearchBar.delegate has already been called out as self
//eventSearchBar is not owned by a UISearchController
self.navigationController?.setNavigationBarHidden(true, animated: true)
eventSearchBar.setShowsCancelButton(true, animated: true)
eventSearchBar.frame.size.height = 68
TitleView.frame.size.height = 132
eventSearchBar.scopeButtonTitles = ["Category", "Project"]
eventSearchBar.showsScopeBar = true
if eventSearchBar.text! == "" {
self.tableView.reloadData()
} else {
if eventSearchBar.selectedScopeButtonIndex == 0 {
self.loadSearchElements(self.refreshControl)
} else {
self.loadProjectSearchElements(self.refreshControl)
}
}
}
Problem solved. Issue is that the storyboard searchBar object has been updated for iOS 13. Solution was to remove the searchBar from the viewController in the storyboard and replace it with a new searchBar, dragged from the objects list. The old and new searchBar look identical, but the connection to their delegate properties is evidently different.

How can the background or the color in the navigation bar be changed?

Currently I couldn't find any method to change the color/background of the navigation bar in SwiftUI. Any tips?
In order to change color of navigation bar for all view controllers, you have to set it in AppDelegate.swift file
Add following code to didFinishLaunchingWithOptions function in AppDelegate.swift
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517)
In here tintColor attribute change the background color of the navigation bar.
barTintColor attribute affect to the color of the:
back indicator image
button titles
button images
Bonus:
Change color of navigation bar title:
// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
titleTextAttributes affect to the title text
I hope it helps. :)
In SwiftUI, at this point we can not change it directly, but you can change navigationBar appearance like this,
struct YourView: View {
init() {
UINavigationBar.appearance().backgroundColor = .orange
//Use this if NavigationBarTitle is with Large Font
UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
//Use this if NavigationBarTitle is with displayMode = .inline
//UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
}
var body: some View {
NavigationView {
Text("Hello World!")
.navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
//.navigationBarTitle (Text("Dashboard"), displayMode: .inline)
}
}
}
I hope this will help you. Thanks!!
Till now there is no definitive API in SwiftUI for this purpose. But you can use the appearance API. Here is a sample code.
import SwiftUI
struct ContentView : View {
init() {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
UINavigationBar.appearance().backgroundColor = .green
}
var body: some View {
NavigationView {
NavigationButton(destination: SecondPage(), label: {
Text("Click")
})
.navigationBarTitle(Text("Title"), displayMode: .inline)
}
}
}
Put a Rectangle behind your NavigationView inside a ZStack:
ZStack {
Rectangle().foregroundColor(.red)
NavigationView {
...
}
}
Please see this answer for a solution that does not use .appearance().
In short use UIViewControllerRepresentable
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfigurator>) {
uiViewController.navigationController?.navigationBar...
}
With Introspect you could do it this way:
NavigationView {
Text("Item 2")
.introspectNavigationController { navigationController in
navigationController.navigationBar.backgroundColor = .red
}
}
One thing to note that I didn't at first understand: SwiftUI will change the appearance of things like NavigationBar based on whether you are in night mode.
If you want to default it to a different color scheme add
.colorScheme(.dark)
If you create a color scheme using the color set system as outlined in this post: https://www.freecodecamp.org/news/how-to-build-design-system-with-swiftui/ it would apply to the main elements like navigations and tab bars, and allow you to apply different schemes for night/day mode.
The NavigationView is managing full screens of content. Each of those screens has its own background color. Therefore you can use the following approach to apply your Background color onto the screens:
let backgroundColor = Color(red: 0.8, green: 0.9, blue: 0.9)
extension View {
func applyBackground() -> some View {
ZStack{
backgroundColor
.edgesIgnoringSafeArea(.all)
self
}
}
}
Then you apply it to all your screens:
NavigationView {
PrimaryView()
.applyBackground()
DetailView(title: selectedString)
.applyBackground()
}
Be aware: some SwiftUI views have their own background color which is overriding yours (e.g. Form and List depending on context)
iOS 16
You can set any color to the background color of any toolbar background color (including the navigation bar) for the inline state with a simple native modifier:
Xcode 14 beta 5 (Not working 🤦🏻‍♂️, waiting for beta 6...)
.toolbarBackground(.yellow, for: .navigationBar)
Xcode 14 beta 1,2,3,4
.toolbarBackground(.yellow, in: .navigationBar)
Note that the color will set on the entire bar (up to the top edge of the screen).
Also, the color will be animated during the transition between Large and Inline modes of the bar.

Editable NSTextFields with Variable Height in NSTableView Rows in macOS App

Xcode 10.1, Swift 4.2, macOS 10.14.2
I am trying to make a simple to do list app for macOS where there are a series of NSTableView rows and inside each one is an NSTextField. Each field is a to-do item. I want the NSTableView rows to expand to fit the size of the text within each NSTextField.
I have all of the following working:
Setting the text in the NSTextField makes the NSTableView row expand as needed. Auto layout constraints are set in my storyboard.
Using tableView.reloadData(forRowIndexes: ..., columnIndexes: ...) sets the text and resizes the table row correctly.
But doing tableView.reloadData() always resets every NSTextField to a single line of text as shown here:
Interestingly, if you click into the NSTextField after reloading the whole table, the field resizes to fit its content again:
I believe I have set all the appropriate auto layout constraints on my NSTextField and I'm using a custom subclass for it as well (from a helpful answer here):
class FancyField: NSTextField{
override var intrinsicContentSize: NSSize {
// Guard the cell exists and wraps
guard let cell = self.cell, cell.wraps else {return super.intrinsicContentSize}
// Use intrinsic width to jibe with autolayout
let width = super.intrinsicContentSize.width
// Set the frame height to a reasonable number
self.frame.size.height = 150.0
// Calcuate height
let height = cell.cellSize(forBounds: self.frame).height
return NSMakeSize(width, height)
}
override func textDidChange(_ notification: Notification) {
super.textDidChange(notification)
super.invalidateIntrinsicContentSize()
}
}
⭐️Here is a sample project: https://d.pr/f/90CTEh
I'm at a loss as to what else I can try. Any ideas?
I think there is some basic problems with the constraints in interface builder. Resizing the window makes everything wonky. Also you should call validateEditing() in the textDidChange(forBounds:) in your FancyField class.
I created a sample project, that does what you want on Github
Write a comment if you have any problems with it.
Thinking a little about it, thought i would add the meat of the code here. Only thing that really needs to work, is the update on the NSTextField when the "Tasks" is being updated. Following is the code required for the NSTextField.
public class DynamicTextField: NSTextField {
public override var intrinsicContentSize: NSSize {
if cell!.wraps {
let fictionalBounds = NSRect(x: bounds.minX, y: bounds.minY, width: bounds.width, height: CGFloat.greatestFiniteMagnitude)
return cell!.cellSize(forBounds: fictionalBounds)
} else {
return super.intrinsicContentSize
}
}
public override func textDidChange(_ notification: Notification) {
super.textDidChange(notification)
if cell!.wraps {
validatingEditing()
invalidateIntrinsicContentSize()
}
}
}
Hope it helps.

hide navigation bar programmatically while application running

I am developing an android game with webview layout. I would like to hide the navigation bar while the user is playing the game. I have found some solutions but when I touch the screen the navigation bar shows up.
What am I doing wrong? Here is my non-working solution:
int mUIFlag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
getWindow().getDecorView().setSystemUiVisibility(mUIFlag);
you need to make use of following piece of code to reset the flags again :
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(mUIFlag);
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});
also, read through this page to know how to handle your scenario

QT/C++ on MAC - How do I hide my dock icon?

I'd like my dock icon to be hidden and the app to be represented as a menu bearing icon on the menu bar (right hand side).
This will hide the icon:
http://www.macosxtips.co.uk/index_files/disable-the-dock-icon-for-any-application.html
Summarizing the URL, in the dict xml element inside Info.plist in the app, add the lines:
<key>LSUIElement</key>
<string>1</string>
You need to use the appropriate OS APIs to add the menu.
The alternative answer is by using TransformProcessType() to show/hide dock icon dynamically in code.
#import <Foundation/Foundation.h>
void toggleDockIcon(bool show) {
ProcessSerialNumber psn = {0, kCurrentProcess};
if (show) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
} else {
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
}
}

Resources