Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. NSView *windowContentView = [mainWindow contentView];
  2. NSRect windowContentBounds = [windowContentView bounds];
  3. scrollView = [[NSScrollView alloc] init];
  4. [scrollView setBorderType:NSNoBorder];
  5. [scrollView setHasVerticalScroller:YES];
  6. [scrollView setBounds: windowContentBounds];
  7. [windowContentView addSubview:scrollView];
  8.  
  9. NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:[[mainWindow contentView] frame]];
  10.  
  11. // configure the scroll view
  12. [scrollView setBorderType:NSNoBorder];
  13. [scrollView setHasVerticalScroller:YES];
  14.  
  15. // embed your custom view in the scroll view
  16. [scrollView setDocumentView:outletToCustomViewLoadedFromNib];
  17.  
  18. // set the scroll view as the content view of your window
  19. [mainWindow setContentView:scrollView];
  20.  
  21. // Initial scrollview
  22. let scrollView = NSScrollView()
  23. scrollView.translatesAutoresizingMaskIntoConstraints = false
  24. scrollView.borderType = .noBorder
  25. scrollView.backgroundColor = NSColor.gray
  26. scrollView.hasVerticalScroller = true
  27.  
  28. window.contentView?.addSubview(scrollView)
  29. window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: [], metrics: nil, views: ["scrollView": scrollView]))
  30. window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: [], metrics: nil, views: ["scrollView": scrollView]))
  31.  
  32. // Initial clip view
  33. let clipView = NSClipView()
  34. clipView.translatesAutoresizingMaskIntoConstraints = false
  35. scrollView.contentView = clipView
  36. scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .left, relatedBy: .equal, toItem: scrollView, attribute: .left, multiplier: 1.0, constant: 0))
  37. scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1.0, constant: 0))
  38. scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .right, relatedBy: .equal, toItem: scrollView, attribute: .right, multiplier: 1.0, constant: 0))
  39. scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 0))
  40.  
  41. // Initial document view
  42. let documentView = NSView()
  43. documentView.translatesAutoresizingMaskIntoConstraints = false
  44. scrollView.documentView = documentView
  45. clipView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .left, relatedBy: .equal, toItem: documentView, attribute: .left, multiplier: 1.0, constant: 0))
  46. clipView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .top, relatedBy: .equal, toItem: documentView, attribute: .top, multiplier: 1.0, constant: 0))
  47. clipView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .right, relatedBy: .equal, toItem: documentView, attribute: .right, multiplier: 1.0, constant: 0))
  48.  
  49. // Subview1
  50. let view1 = NSView()
  51. view1.translatesAutoresizingMaskIntoConstraints = false
  52. view1.wantsLayer = true
  53. view1.layer?.backgroundColor = NSColor.red.cgColor
  54. documentView.addSubview(view1)
  55. documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view1]|", options: [], metrics: nil, views: ["view1": view1]))
  56.  
  57. // Subview2
  58. let view2 = NSView()
  59. view2.translatesAutoresizingMaskIntoConstraints = false
  60. view2.wantsLayer = true
  61. view2.layer?.backgroundColor = NSColor.green.cgColor
  62. documentView.addSubview(view2)
  63. documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view2]|", options: [], metrics: nil, views: ["view2": view2]))
  64.  
  65. // Subview3
  66. let view3 = NSView()
  67. view3.translatesAutoresizingMaskIntoConstraints = false
  68. view3.wantsLayer = true
  69. view3.layer?.backgroundColor = NSColor.blue.cgColor
  70. documentView.addSubview(view3)
  71. documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view3]|", options: [], metrics: nil, views: ["view3": view3]))
  72.  
  73. // Vertical autolayout
  74. documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view1(==100)][view2(==200)][view3(==300)]", options: [], metrics: nil, views: ["view1": view1, "view2": view2, "view3": view3]))
  75. documentView.addConstraint(NSLayoutConstraint(item: documentView, attribute: .bottom, relatedBy: .equal, toItem: view3, attribute: .bottom, multiplier: 1.0, constant: 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement