Rounded corners of NSCollectionViewItem - swift

Simple question, but I can't find any answers... How I can change corner radius of NSCollectionViewItem instance?

view of NSCollectionViewItem doesn't have layer by default. You need to set wantsLayer to true, for example:
import Cocoa
class TestCellItem: NSCollectionViewItem {
override func viewDidLoad() {
super.viewDidLoad()
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.red.cgColor
}
override func viewDidLayout() {
super.viewDidLayout()
view.layer?.cornerRadius = 20
}
}

Related

Unable to add constraints to my NSTableView in Cocoa Application

Below is my code in view controller
import Cocoa
class ViewController : NSViewController, NSTableViewDelegate, NSTableViewDataSource {
let stepperBtn: NSSegmentedControl = {
let stepperBtn = NSSegmentedControl()
stepperBtn.segmentCount = 2
stepperBtn.setImage(NSImage(systemSymbolName: "plus.circle", accessibilityDescription: "plus"), forSegment: 0)
stepperBtn.setImage(NSImage(systemSymbolName: "minus.circle", accessibilityDescription: "minus"), forSegment: 1)
stepperBtn.translatesAutoresizingMaskIntoConstraints = false
return stepperBtn
}()
var tableView: NSTableView {
let tablview = NSTableView()
tablview.translatesAutoresizingMaskIntoConstraints = false
return tablview
}
override func loadView() {
self.view = NSView(frame: NSMakeRect(0.0, 0.0, 550.0, 300.0))
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
self.view.addSubview(stepperBtn)
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
}
override func viewDidLayout() {
}
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfRows(in tableView: NSTableView) -> Int {
return 5
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
I get below error : "NSTableView:0x7f9beb851600.top"> and <NSLayoutYAxisAnchor:0x600000bcc400 "NSView:0x7f9beaa184e0.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.
How to add constraints programatically to NSTableView

Back Button Tint Color

How can I change the back button of a certain navigation controller. I have tried to use
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.tintColor = UIColor.red
}
I know that if i use navigationController it will change the back button tint color on all of my view controllers.
Try this!!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = .red
}
override func willMove(toParent parent: UIViewController?) {
self.navigationController?.navigationBar.tintColor = // original color
}
}

How to use “Look Up in Dictionary” in NSPopover Swift

I'm building my macOS app that contains NSPopover with NSTextView inside. If "Look up & data detectors" is active at system preferences, NSTextView will support this functionality without any additional settings. But in case, when you place NSTextView inside of NSPopover, this function won't work anymore. Is there any way to fix this problem without NSTextView.showDefenition(for: at:)?
How it usually works
Same thing, but won't work with NSTextView that placed inside of NSPopover
I've started new test project just to check behaviour, so code is simple as it is.
Main Controller:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var popoverButton: NSButton!
#IBAction func pressButton(_ sender: NSButton) {
let popover = NSPopover()
popover.contentViewController = PopoverController()
popover.contentSize = CGSize(width: 470, height: 300)
popover.show(relativeTo: popoverButton.bounds, of: popoverButton, preferredEdge: NSRectEdge.minY)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Popover Controller:
import Cocoa
class PopoverController: NSViewController {
var textView = NSTextView()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear() {
super.viewWillAppear()
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.frame = view.bounds
}
}

Passing TableView Data to DetailViewController in swift

I have a simple tableview loading with array of colours(each cell with different colour).i am trying to pass the background colour of detailViewController when user press the colour array cell from colourTableviewcontroller(simply i want to pass the cell colour as a detailview background colour)
my code as follows.....
import UIKit
class colourTableviewcontroller: UITableViewController {
#IBOutlet weak var colorTableView: UITableView!
let colors = [UIColor.redColor(), UIColor.blueColor(),UIColor.greenColor(), UIColor.orangeColor(),UIColor.purpleColor(),UIColor.yellowColor(),UIColor.cyanColor(),UIColor.darkGrayColor(),UIColor.blackColor(), UIColor.brownColor(),UIColor.grayColor(), UIColor.magentaColor(), UIColor.whiteColor()]
var colourData = UIColor()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
colorTableView.separatorColor = UIColor.clearColor()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return colors.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("colourCell", forIndexPath: indexPath) as UITableViewCell
cell.backgroundColor = self.colors[indexPath.row % self.colors.count]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
colourData = self.colors[indexPath.row]
performSegueWithIdentifier("colourSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "colourSegue") {
let viewController = segue.destinationViewController as! DetailViewController
viewController.bc?.backgroundColor = colourData }
}}
// My DetailViewController as follows...
import UIKit
class DetailViewController: UIViewController {
#IBOutlet var bc: UIView!
override func viewDidLoad() {
self.bc.backgroundColor = UIView.appearance().backgroundColor
}
when i run the code my DetailViewController background colour changing to dark text colour when i press the colour array.....
i don't know what i am missing or am i doing wrong approach/logic....
Thanks in Advance.....
First of all in ColourTableviewcontroller connect the segue to the table view cell rather than to the view controller and delete the method didSelectRowAtIndexPath
Replace prepareForSegue with
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "colourSegue" {
let viewController = segue.destinationViewController as! DetailViewController
let selectedIndexPath = tableView.indexPathForCell(sender as! UITableViewCell)
viewController.color = colors[selectedIndexPath.row]
}
}
In DetailViewController declare a variable color
var color : UIColor!
And set the color of bc in viewDidLoad()
bc.backgroundColor = color
The reason for the additional variable is that the IBOutlets in DetailViewController don't exist yet while prepareForSegue is executed.
You aren't passing the UIColor and assigning it in the next View Controller
You just need to do few modifications to your existing code
1) Add var recievedColor : UIColor! in the DetailViewController
2) In your prepareForSegue replace viewController.bc?.backgroundColor = colourData with viewController.recievedColor = colourData
3) In your DetailViewController, in viewDidLoad() function, replace self.bc.backgroundColor = UIView.appearance().backgroundColor with self.bc.backgroundColor = recievedColor
And there you go!
You need to pass the color, then assign the color to the view on the viewDidLoad(). You are assigning a variable to a view that hasn't been initialized yet.
in your detail class do as follow:
class DetailViewController: UIViewController {
#IBOutlet var bc: UIView!
var selectedBackgroundColor: UIColor? // this should be set on the prepareForSegue method.
override func viewDidLoad() {
super.viewDidLoad()
if let selectedBackgroundColor = selectedBackgroundColor {
bc.backgroundColor = selectedBackgroundColor
}
}
Hope this helps!

Add a NSVisualEffectView below NSTextView

I want to add a NSVisualEffectView inside my NSTextView, however when I add it, the text is below the NSVisualEffectView, so, how can i add the NSVisualEffectView below the text?
My code for OS X:
class myTextView: NSTextView {
override func awakeFromNib() {
let visualEffectView = NSVisualEffectView(frame: NSMakeRect(20, 20, 30, 18))
visualEffectView.material = NSVisualEffectMaterial.Dark
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
self.addSubview(visualEffectView)
}
}
Maybe this will help you:
class MyTextView: NSTextView {
lazy var visualEffectView: NSVisualEffectView = {
let visualEffectView = NSVisualEffectView()
visualEffectView.material = NSVisualEffectMaterial.Dark
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
return visualEffectView
}()
override func awakeFromNib() {
super.awakeFromNib()
self.drawsBackground = false
}
override func viewDidMoveToSuperview() {
super.viewDidMoveToSuperview()
if let containerView = self.superview?.superview?.superview {
containerView.addSubview(self.visualEffectView, positioned: .Below, relativeTo: self)
}
}
override func resizeSubviewsWithOldSize(oldSize: NSSize) {
super.resizeSubviewsWithOldSize(oldSize)
if let superview = self.superview?.superview {
self.visualEffectView.frame = superview.frame
}
}
}
It's a text view in a scroll view and clip view so you have to get its superview.
self.sendSubviewToBack(visualEffectView)
But the visual effect needs to be applied to the parent container, not the text view itself, so if I'm understanding your code correctly you need to move this whole thing up to whatever the parent view is.