Posts

Post marked as solved
2 Replies
616 Views
(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)
Posted Last updated
.
Post not yet marked as solved
2 Replies
503 Views
(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
Posted Last updated
.
Post marked as solved
5 Replies
1.3k Views
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()         })
Posted Last updated
.
Post marked as solved
6 Replies
2.9k Views
(macOS, Swift, storyboards)Can I change a label on hover?WHAT I HAVE TRIED1- I have put a button in storyboard and make it transparent. It works if I click, but I could nof find how to if hover@IBOutlet weak var label1: NSTextField!@IBAction func button1(_ sender: NSButton) { label1.textColor = NSColor.gray}2- I have put a NSView and drag a NSClickGestureRecognizer to it. Again, it works on click but I cannot find a way to do it with hover@IBOutlet weak var label2: NSTextField!@IBAction func gestureRecognizer1(_ sender: NSClickGestureRecognizer) { label2.textColor = NSColor.gray}I could find something in Apple documentation. But it talks about UIButton. I cannot find a way to adapt to my case:https://developer.apple.com/documentation/uikit/uihovergesturerecognizer
Posted Last updated
.
Post marked as solved
5 Replies
3.1k Views
How to make a link in a label or a text view? I could find different solutions for iOS but not for macOS. For instance, this works in iOS but not in macOS: 				let attributedString = NSMutableAttributedString(string: "Just click here to register") 				let range = NSRange(location: 5, length: 10) 				let url = URL(string: "https://www.google.com")! 				attributedString.setAttributes([.link: url], range: range) 				textView1.textStorage?.setAttributedString(attributedString) 				textView1.linkTextAttributes = [ 						.foregroundColor: NSColor.blue, 						.underlineStyle: NSUnderlineStyle.styleSingle.rawValue 				] In macOS it gives me two errors: Value of type NSScrollView has no member textStorage    Value of type NSScrollView has no member linkTextAttributes
Posted Last updated
.
Post marked as solved
2 Replies
363 Views
I have an NSView and I want it to be half of the window. This is what I have tried: In storyboard I dragged a Custom View I give it a Constraint of 0 in Top, Left and Bottom How can I make it half of the screen even when the user changes the size of the screen?  @IBOutlet weak var nsView1: NSView! nsView1.wantsLayer = true nsView1.layer?.backgroundColor = NSColor.red.cgColor
Posted Last updated
.
Post not yet marked as solved
2 Replies
311 Views
macOS, Swift, User Interface: Storyboard I have two pages: ViewController and Preferences. I have a button on the Preferences window and I want to trigger an action on the ViewController window. How can I do that? The button is in Preferences window and the action should be in the ViewController window. (In storyboard, If I open the Assistant Editor I get Preferences.swift. I cannot make the regular connection with the control drag)
Posted Last updated
.
Post not yet marked as solved
8 Replies
561 Views
When I Run a project I see nothing, the window does not appear. I see no error. I see the "Build succeed" and the general menu of the bar, but no window.Even when I create a new project, with no code, sometimes I see the window, sometimes I do not. When I see the window, if I add some code, just a few lines, then run, and I cannot see the window anymore. I tried many things and I do not see a pattern, it seems random. I tried to give a different name, use the shortcut, use the button, I am sure that is not hidden behind or something, I compare a project that works and one that does not and I see no difference ...I can do no work and I have no idea what's going on. Any suggestion?macOS Catalina 10.15.5Xcode 11.5Swift
Posted Last updated
.
Post not yet marked as solved
1 Replies
1.1k Views
Is it possible to have a small WebView? for instance 200 x 250I have a Webkit View from the LibraryIn the ViewController: import Cocoa import WebKit class ViewController: NSViewController { @IBOutlet weak var webView1: WKWebView! override func viewDidLoad() { super.viewDidLoad() let url = URL(string: "https://www.apple.com") let request = URLRequest(url:url!) webView1.load(request) }I can see the content of the web site but it is cut. I do not need a full web browser only an image representation of the site.
Posted Last updated
.
Post marked as solved
4 Replies
474 Views
macOS, Swift, storyboardsI have a label with minutes and seconds of a countdown. 00:00I want it horizontally centered in the container and at a certain distance from the top. When I put those constraints, all the label moves when there is a new second as it tries to center: 00:01, 00:02... it creates and strange effect. I suppose that the correct behaviour would be to have the two points fixed in the center. How to use constraints in this case?
Posted Last updated
.
Post marked as solved
8 Replies
4.4k Views
macOS, Storyboards, Swift.I have a button to perform a segue to a second page. I can make a control drag from the button to the second page. In Attributes, I have selected a Kind Sheet. Is it possible to open it with a fade in?To make things easy let's say that:The first window is ViewController.swiftThe second window is SecondViewControllerI have button1 in ViewController.swift with segue1 that goes to SecondViewController.swift
Posted Last updated
.
Post marked as solved
2 Replies
696 Views
macOS, Swift, StoryboardsI have two windows. I have opened the second window with a Storyboard Segue Kind Sheet. I want to close that window with a fade out. I put that in SecondViewController.swift. This works well to make the fade out. But, if I understand, the window is not visible but it is still there and I cannot click any button behind. How to solve that?(If I use dismiss(self) I close the window but without the fade out. I want fade out, and then close) @IBAction func close1(_ sender: NSButton) { self.view.window?.animationBehavior = .none self.view.window?.makeKeyAndOrderFront(nil) NSAnimationContext.runAnimationGroup({ (context) -> Void in context.duration = 2 self.view.window?.animator().alphaValue = 0 }, completionHandler: nil) // dismiss(self) }
Posted Last updated
.
Post marked as solved
4 Replies
4.0k Views
I update one field in Core Data. This code works well:func updateData(){ guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return } let managedContext = appDelegate.persistentContainer.viewContext let fetchRequest:NSFetchRequest = NSFetchRequest.init(entityName: "User") fetchRequest.predicate = NSPredicate(format: "username = %@", "John") do { let test = try managedContext.fetch(fetchRequest) let objectUpdate = test[0] as! NSManagedObject objectUpdate.setValue("John2", forKey: "username") objectUpdate.setValue("John2@example.com", forKey: "email") objectUpdate.setValue("newPassword", forKey: "password") do { try managedContext.save() } catch { print(error) } } catch { print(error) } }I would like to add a way to check if what I try to update exists. In that specific case, I want to check first if I have in the database the username John
Posted Last updated
.
Post not yet marked as solved
11 Replies
1.4k Views
What is the right way to insert or write today's date in Core Data?in .xcdatamodeld I have the Entity Contingut and the Attributes:dateCreated of Type Datetext1 of Type String func today1() { let date = Date() let calendar = Calendar.current let day = calendar.component(.day, from: date) let month = calendar.component(.month, from: date) let year = calendar.component(.year, from: date) let avui = "\(year)-\(month)-\(day)" print (avui) } // CREATE @IBAction func createData(_ sender: UIButton) { guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return } let managedContext = appDelegate.persistentContainer.viewContext let contingutEntity = NSEntityDescription.entity(forEntityName: "Contingut", in: managedContext)! let contingut = NSManagedObject(entity: contingutEntity, insertInto: managedContext) contingut.setValue(today1(), forKey: "dateCreated") contingut.setValue("some text", forKey: "text1") do { try managedContext.save() } catch let error as NSError { print("Could not save. \(error), \(error.userInfo)") } }When I create a new text1, I want to register the day. What is the right way to do that?
Posted Last updated
.
Post marked as solved
6 Replies
1.6k Views
Swift, Storyboards, Core DataI have this code that makes an insert with Core Data. How would it be read, update and delete in this specific case?I have seen a lot of tutorials and each one makes things a bit different. I could not adapt to this case.(I have already created the data model with the entity Users and the Attrituves name and password. I also have linked the buttons read, update and delete. I am looking for the most bàsic, without tables... Just like the insert I have here. Thank you!)import UIKit import CoreData class ViewController: UIViewController { @IBOutlet weak var name: UITextField! @IBOutlet weak var password: UITextField! lazy var context : NSManagedObjectContext = { let appDelegate = UIApplication.shared.delegate as! AppDelegate return appDelegate.persistentContainer.viewContext }() // INSERT @IBAction func button(_ sender: UIButton) { guard name.hasText && password.hasText else { return } let user = Users(context: context) user.name = self.name.text! user.password = self.password.text! do { try context.save() print ("saved") } catch { print(error) } } // READ @IBAction func readButton(_ sender: UIButton) { } // UPDATE @IBAction func updateButton(_ sender: UIButton) { } // DELETE @IBAction func deleteButton(_ sender: UIButton) { } }
Posted Last updated
.