NSWindow Transparent Background NSCollectionView background not repainting
After much trial and error (more error) I found the solution.
In the NSWindow subclass if you set the background color this gets rid of the clear "holes" in the background.
import Cocoa
class MainWindow: NSWindow {
override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: false)
self.backingType = NSWindow.BackingStoreType.buffered
self.alphaValue = 1.0
self.backgroundColor = NSColor(calibratedRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.2)
self.isOpaque = false
}
}
The ViewController also required a few things
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var collectionView: NSCollectionView!
@IBOutlet weak var scrollView: NSScrollView!
override func viewDidLoad() {
super.viewDidLoad()
configureCollectionView()
collectionView.register(ReceiverCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"))
}
private func configureCollectionView() {
let flowLayout = NSCollectionViewFlowLayout()
flowLayout.itemSize = NSSize(width: 100.0, height: 100.0)
flowLayout.sectionInset = NSEdgeInsets(top: 10.0, left: 20.0, bottom: 10.0, right: 20.0)
flowLayout.minimumInteritemSpacing = 20.0
flowLayout.minimumLineSpacing = 20.0
collectionView.collectionViewLayout = flowLayout
scrollView.drawsBackground = false
scrollView.contentView.drawsBackground = false
scrollView.backgroundColor = NSColor.clear
collectionView.backgroundColors = [.clear]
}
}
extension ViewController: NSCollectionViewDataSource {
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), for: indexPath)
return item
}
}
How to Color the Overshoot Background of an NSCollectionView
- In Interface Builder, select the CollectionView in the document outline.
- In the Utilities window, show the Attributes inspector.
- Set the desired color in the Primary color selector control.
Note: the Ray Wenderlich tutorial that you referenced sets the background color of the collection view programmatically. You'll either want to remove that line, or ensure that you are setting the same color that you chose in step 3 above.
Changing the background color of an NSCollectionView
In .m, remove this line :
[self setBackgroundColors:[NSArray arrayWithObjects:[NSColor blueColor], nil]];
And use this code:
- (void)drawRect:(NSRect)dirtyRect{
[[NSColor blueColor] setFill];
NSRectFill(dirtyRect);
}
Also don't forget to change class of NSCollectionView object in IB to CollectionViewBg.
Hope this helps :)
NSScrollview and transparent, overlay NSScroller subclasses
I was able to get the positioning by implementing tile
in the subclass OF NSSCROLLVIEW
- (void)tile {
[super tile];
[[self contentView] setFrame:[self bounds]];
}
And it turns out that trying to draw clear color was the problem to begin with. In the scroller subclass, I just omitted any drawing that had to do with the slider track completely BY OVERRIDING DRAWRECT:
OF THE NSSCROLLER SUBCLASS, LIKE SO:
- (void)drawRect:(NSRect)dirtyRect
{
[self drawKnob];
}
Related Topics
Nstextalignment.Justified for Uilabel Does Not Work
Nsbundle.Mainbundle().Urlforresource("Bach1", Withextension: "Jpg") Returning Null
Swift: Extract Float from Byte Data
Storyboard Tableview with Segues to Multiple Views
Swift Lazy Subscript Ignores Filter
Swift 4 Codable - API Provides Sometimes an Int Sometimes a String
Vapor Toolbox Broken After Upgrading Swift
How to Succinctly Get the First 5 Characters of a String in Swift
Convert Firdatasnapshot to Custom Type
Cannot Use Mutating Member on Immutable Value of Type
How to Get Day and Month from Date Type - Swift 4
Pause an Skaction in Spritekit with Swift
How to Take Screen Shot Programmatically (Swift, Spritekit)
Access Properties via Subscripting in Swift
How to Provide Default Implementations for Uipageviewcontrollerdatasource