I change the text of a label dynamically (so I do not know the text of the label) how can I animate the size to fit. I am looking for a smooth, elegant transition for design purposes.
What I have tried:
NSAnimationContext.runAnimationGroup({ (context) in
context.duration = 4.0
label1.stringValue = "long, long text"
label1.animator().sizeToFit()
})
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
(swift, macOS, storyboards)
I open a window with code. Every time the user clicks the button the same window opens again and again. How to avoid that?
This opens well the window, but multiple times:
@IBAction func obrirAmbCodi(_ sender: NSButton) {
let storyboard:NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
guard let controller:NSWindowController = storyboard.instantiateController(withIdentifier: "finestra3") as? NSWindowController else { return }
controller.showWindow(self)
}
If I open the window with a segue (storyboard control drag) I can accomplish that by selecting the window controller > Attributes > Presentation > Single. But it seems that it does not work if I open the window with code
(swift, storyboards, macOS)
Can I stop or pause an animation?
(I could find how to remove it:
view1.layer?.removeAllAnimations()
Here is the official documentation but I do not understand:
https://developer.apple.com/documentation/uikit/uiviewanimating/1649843-pauseanimation)
@IBAction func fadeOut(_ sender: NSButton) {
NSAnimationContext.runAnimationGroup({ (context) in
context.duration = 5.0
view1.animator().alphaValue = 0
})
}
(Swift, macOS, storyboards)
I try to make a simple page with an scroll, text and images. Similar to help pages in all mac programs. For instance, TextEdit help:
I tried many things. For instance:
Scroll View with constraints 0 to top, bottom, leading, trailing
Inside the Scroll View: a Label, an Image View, another Label. The labels with constraints 20px top, bottom, leading, trailing.
Labels Priority 750. The rest of constraints Priority 1000
With this, the user can change the width and height of the window, the text adapts but if the window is smaller than the content, I do not see the scroll. The attributes of the scroll are Show Vertical Scroller and Automatically Hide Scroller. If I change one of the options, I do not see the scroll space (or I see the scroll but not the movable part of the scroll)
How to have text, images, a window that can change width, height, and a scroll that appears when the content is bigger than the window. (Like a regular web page or the help that Apple has in many programs)?
(macOS, Swift, storyboard)
Is it possible to have buttons in the title bar? I would like the same buttons that Apple uses in the preferences
(Swift, macOS, storyboards.)
I have a window that is semitransparent and a NSView that I want to be not transparent or with a different transparent degree.
With my code, if I make the window transparent the NSView is also transparent.
func finestra() {
self.view.window?.alphaValue = 0.7
}
func nsView1() {
nsViewOutlet.wantsLayer = true
nsViewOutlet.layer?.backgroundColor = NSColor.white.cgColor
nsViewOutlet.alphaValue = 1
}
I want my program to do something at a specific time. This is what I have tried:
func rellotge() {
countdownRellotge = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(actualitzaRellotge),
userInfo: nil, repeats: true)
}
@objc func actualitzaRellotge() {
let date = Date()
let calendar = Calendar.current
hour = calendar.component(.hour, from: date)
minutes = calendar.component(.minute, from: date)
seconds = calendar.component(.second, from: date)
//print (minutes, ":", seconds)
if (minutes == 40 && seconds == 0){
print ("do something")
}
}
This does not work if the computer goes to sleep. Even before that, it becomes buggy. (When I print the seconds, it seems that sometimes it skips some seconds)
How can I reliably make my program do something at a specific minute-second?
Xcode 16.2
Swift 6
macOS Sequoia 15.1
SwiftUI
I am a beginner.
If I understand we can add buttons to the default Menu Bar but not delete them. Am I right? If you do not need most of the buttons, how do you solve that?
I can add a button:
import SwiftUI
@main
struct NouMenuProvesApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
CommandMenu("Custom") {
Button("Custom Action") {
print("Custom Action performed")
}
}
}
}
}
Can I delete a Menu Button or create a new simpler Menu Bar?
macOS, SwiftIs it possible to remove the scroll in WebKit View? (or make it not visible)I have a WebKit View from library and I have made the connection to the ViewController.swift:@IBOutlet weak var webView1: WKWebView!
macOS, Swift, storyboards
How can I make the window active in all spaces?
I could find how to do that with storyboard: Select the window > Attributes > Spaces > Can join all spaces. How can I do the same with code (activate and de-activate)
How to make a link when I click to a button in macOS?
This works well in iOS. How to do the same with macOS:
@IBAction func button1(_ sender: UIButton) {
if let url = URL(string: "some link") {
UIApplication.shared.open(url)
}
}
(swift, macOS, storyboards)
I can animate a window just adding animate: true to a regular way to control a window. Is there any way to control the animation, especially the time the animation will take?
self.view.window?.setFrame(NSRect(x:0, y:0,
width:300, height:300),
display: true, animate: true)
(Swift, macOS, storyboards)
I can find the total height of the screen:
let totalHight = NSScreen.main?.frame.height
To know the real space I have to subtract 23 px for the Menu Bar.
let usableHight = totalHight - 23
But on some occasions the user may not have the Menu Bar active (System Preferences > General > uncheck Automatically hide and show the menu bar)
Is there a way to know if the Menu Bar is hidden or show? or perhaps there is another way to know the real usable height of the screen?
(Swift, macOS, Storyboards)
I can read a JSON from an URL. I would like to know when I can access the information. To be able to go from one function after the other. It seems that "completion" does not work with JSON? or perhaps I am doing something wrong?
I give a simplified example: I try to get the JSON information in func first() and then do something with that information in func second()
var result2 = "empty"
func first(completion: ()->()) {
let url = URL(string: "https://jsonplaceholder.typicode.com/users")!
URLSession.shared.dataTask(with: url) { data, _, _ in
if let data = data {
let result = try? JSONDecoder().decode([User].self, from: data)
self.result2 = result![0].name
}
}.resume()
completion()
}
func second() {
//do something after first()
print (result2)
}
@IBAction func button1(_ sender: NSButton) {
first {
second()
}
}
(Swift, macOS, Storyboards)
How to open a View with code and macOS? (a View, not a Window)
I can open a Window Controller:
var controller: NSWindowController?
if controller == nil {
let storyboard:NSStoryboard = NSStoryboard(name: "Main", bundle: nil)
controller = storyboard.instantiateController(withIdentifier: "window1") as? NSWindowController
}
controller?.showWindow(self)
But I could not figure out how to open a View Controller. What would the be equivalent code for a View Controller in macOS?