包含NSStackView的NSScrollView.为什么NSStackView项从下到上? [英] NSScrollView containing NSStackView. Why the NSStackView items from bottom to Top?
问题描述
包含NSStackView的NSScrollView. NSStackView的项目将在运行时添加.新的NSStackView的项目从下至上,但我希望它们从上至下.
The NSScrollView containing a NSStackView. The NSStackView's items will add in runtime.The new NSStackView's items are from bottom to top, but I want they are from top to bottom.
main.storyboard
The main.storyboard
ViewController
The ViewController
import Cocoa
class ViewController: NSViewController {
var todoList:[Todo] = []
@IBOutlet weak var todosStackView: NSStackView!
@IBOutlet weak var todosScrollView: NSScrollView!
@IBAction func onEnter(sender: NSTextField) {
let todo = Todo()
todo.content = sender.stringValue
self.todoList.append(todo)
let todoItemView = TodoItem()
todoItemView.setButtonType(.SwitchButton)
todoItemView.todo=todo
todosStackView.addView(todoItemView, inGravity: .Top)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
复选框按钮类
import Cocoa
class TodoItem: NSButton {
var todo:Todo!{
didSet {
self.title = self.todo.content
self.state = self.todo.done ? NSOnState : NSOffState
}
}
func completeTodo(){
if(self.state == NSOnState){
self.todo.done = true
}else{
self.todo.done = false
}
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.setButtonType(.SwitchButton)
self.target = self
self.action = #selector(self.completeTodo)
}
}
推荐答案
默认情况下,NSClipViews(和基本NSView)不会翻转,因此坐标(和滚动视图的位置)相对于左下角.
By default, NSClipViews (and the base NSView) are not flipped and so coordinates (and scroll view positioning) are relative to the bottom left.
您可以继承NSClipView的子类并覆盖isFlipped
以返回true
.然后在IB中,将滚动视图的剪辑视图设置为该自定义类.
You can subclass NSClipView and override isFlipped
to return true
. Then in IB, set the clip view of the scroll view to that custom class.
这篇关于包含NSStackView的NSScrollView.为什么NSStackView项从下到上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!