I would like to know if it is possible to add a toggle button to a tabbed pane. Here is some code to explain the issue:
TabbedPane {
showTabsOnActionBar: false
Tab {
title: qsTr("Some tab") + Retranslate.onLocaleOrLanguageChanged
//Adding a toggle button here causes an error
}
}
I would like the tab to display "Some tab" and next to it there should be a toggle button. The toggle button would represent some setting. If this is not possible, how would you suggest that I go about doing this?
The other tab can be this:
Tab {
title: "Tab 2"
property variant toggle: false
onTriggered: {
console.log("YOU TRIGGERED! " +toggle)
title = "Tab X"
toggle = !toggle;
}
}
Now you can change image and title from there, and everything the button does.
Related
I have a menu and want to be able to input some data from the menu. This is what I've tried and it shows up in the menu, but doesn't allow any input. Is there a way to hack some input in SwiftUI Menus?
Menu("Award Users") {
TextField(awardedAmount, text: $awardedAmount)
Button("Send") {
}
}
SwiftUI will simplify the layout of the menu items, and if not possible, it may discard some of your items.
Not all views are suitable to work as a menu item and they will be silently ignored. These are some of the views that work: Menu, Text, Button, Link, Label, Divider or Image.
SwiftUI Menu gives us a dedicated view for showing popup button with the help of menus. Menu option is used to create variety of buttons to control what you want to appear in the menu. If you add a textfield, Menu View will consider it as button title and disable its action as shown in image below.
struct ContentView: View {
#State private var awardedAmount = "125"
var body: some View {
VStack{
Menu("Options") {
TextField(awardedAmount, text: $awardedAmount)
Button("Order Now", action: placeOrder)
Button("Adjust Order", action: adjustOrder)
Button("Cancel", action: cancelOrder)
}
}
}
func placeOrder() { }
func adjustOrder() { }
func cancelOrder() { }
}
So in short Menu is a dedicated view for buttons for selecting an option, not taking user inputs.
I am trying to implement a Picker embedded inside a Menu in SwiftUI, following this post. However, there seems to be a bug where the picker does not initially show a checkmark next to the currently selected option when embedded inside a Menu.
Menu {
Picker("Unit", selection: $food.servingSize.unit) {
ForEach(units) { unit in
Text(unit.name).tag(unit)
}
}
} label: {
Text(food.servingSize.unit.shorthand)
}
However, if I don't use Menu and just the Picker by itself instead, the default selection works as expected:
Picker("Unit", selection: $food.servingSize.unit) {
ForEach(units) { unit in
Text(unit.name).tag(unit)
}
}
.pickerStyle(.menu)
Obviously I want to use the Menu in order to change what is being displayed after selection from the Picker. What am I missing here?
I have a NSPopover that contains two buttons. When I open the popover, the following code works to change the cursor to a pointing hand when hovering over the buttons and on clicking the button, 'Button pressed' appears in the console and an NSColorPanel appears as is desired.
class Button: NSButton {
override func resetCursorRects() {
super.resetCursorRects()
addCursorRect(bounds, cursor: .pointingHand)
}
}
#IBAction func buttonTapped(action:Any) {
print("Button pressed")
let cp = NSColorPanel.shared
cp.setTarget(self)
NSColorPanel.setPickerMode(.none)
cp.setAction(#selector(colorDidChange))
cp.isContinuous = false
cp.level = NSWindow.Level.statusBar
cp.makeKeyAndOrderFront(self)
}
However if I click anywhere else on the screen, and then go back to the NSPopover, the pointing hand cursor no longer appears when hovering over the button, and while the onClick event is still fired (as evidenced by 'Button pressed' logged in the console), the NSColorPanel doesn't open.
Any suggestions as to why this might be the case?
I ran into this issue and what solved it for me was adding the below code to the popover view controller viewWillAppear function.
NSApplication.shared.activate(ignoringOtherApps: true)
I've created a menu bar app, a NSMenu object using the Interface Builder (following this tutorial). The menu has two items:
Start Commando
Stop Commando
How can I disable/enable the menu items when they're clicked? I've set disabled "Auto Enables Items" and I can manually enable/disable the items in the Attributes inspector, but how can I achieve the same thing when their functions are called?
When "Start Commando" is clicked I want the item to disable and "Stop Commando" to enable. And the other way around when "Stop Commando" is clicked.
Swift provides with setEnabled property that can be used on NSMenuItem you are trying to enable or disable.
You can do the following :
#IBOutlet weak var startMenuItem: NSMenuItem!
startMenuItem.isEnabled = false or true
You can try below code :
let menu = NSMenu();
menu.autoenablesItems = false
As others say, there is a isEnabled property for NSMenuItems. One also needs to uncheck Auto Enables Items for that menu or sub-menu in the Attributes Inspector in Xcode, or through code, to allow the setting to take effect.
To get it to change on selection, in the IBAction called for the menu item, likely in your NSWindowController, do something like this:
#IBAction private func myMenuAction(sender: NSMenuItem) {
sender.isEnabled = false
}
You will not be able to then select the menu item afterwards. I assume you re-enable it else where as so:
if let appDelegate = NSApplication.shared.delegate as? AppDelegate {
appDelegate.myMenuItem.isEnabled = true
}
Code untested.
Declare a BOOL value for instance
BOOL isActive
if(isActive)
{
//show menu
}
else
{
//hide your menu
}
also make BOOL true when your view dismiss
There is a navigation in my project which I want to config it's navigation back item.
First case: When the UINavigation title is long the title of back button is set to "back"
replacing back button title to "back"
Second case: when it is longer this "back" is not shown.
delete back button title and just show back icon
But I want to show just back icon in the first case too.
Swift 3 - Through Storyboard:
To make navigation bar back button have only back arrow and no "Back" text written, follow the steps:
Go to navigation bar of the root view controller(titled "Home" in screenshot below)
Go to its attribute inspector. Set the back button to a space as shown below:
And that's it!!
This is the simulator screenshot:
Hope it helps!
Add this in viewDidLoad of ViewController which is pushing next ViewController (View Controller Linguini Arabbiatta)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
It will show just a back icon in all the View Controllers which are pushed from that View Controller.
Try this code
if let title = self.navigationController?.navigationBar.backItem?.title {
if title.characters.count > 5 {
self.navigationController?.navigationBar.backItem?.title = "Anything Else"
}
}
Here's Objective-C
[self.navigationItem setHidesBackButton:YES];
Here's Swift
self.navigationItem.setHidesBackButton(true, animated:true);
Edit: if you wish to remove the text only, then here's Objective-C
[self.navigationItem.title = #""];
Here's Swift
self.navigationItem.title = ""
Then you'll need to refresh the nav bar
self.navigationController?.navigationBar.setNeedsLayout()
self.navigationController?.navigationBar.setNeedsDisplay()