NSScrollView containing NSStackView. Why the NSStackView items from bottom to Top?

半城伤御伤魂 提交于 2019-12-07 06:36:50

问题


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.

The main.storyboard

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.
        }
    }
}

The checkbox button class

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)
    }
}

回答1:


By default, NSClipViews (and the base NSView) are not flipped and so coordinates (and scroll view positioning) are relative to the bottom left.

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.




回答2:


You have your view (the parent of stack view) anchored to parent clip view via leading and bottom anchor. Try using leading and top instead. Note: your stack view itself seems to be aligned properly.



来源:https://stackoverflow.com/questions/38680403/nsscrollview-containing-nsstackview-why-the-nsstackview-items-from-bottom-to-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!