Tôi có một số chế độ xem mà tôi cần phải cộng tác với một lớp con tùy chỉnh của NSScrollView mà tôi đang tạo. Một loại chế độ xem cần được sửa hoàn toàn tương đối so với cuộn trong NSScrollView và loại khác cần phải có vỏ bên ngoài cố định ở vị trí tương đối với cuộn nhưng cho phép nội dung bên trong cuộn khi thay đổi scrollview (như tiêu đề cột trong bảng tính ví dụ).Nơi để thêm subViews mà tôi muốn thả nổi trong một NSScrollView sử dụng AutoLayout?
Sau đây là những tùy chọn cho vị trí của những quan điểm trong tầm nhìn thứ bậc AFAIK:
- subview của NSScrollView (làm cho ý nghĩa nhất nhưng tôi nhận được lỗi sau:
*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs)
- subview của superview của NSScrollView (điều này có vẻ hoạt động tốt hơn nhưng không cảm thấy đúng từ quan điểm thiết kế cho rằng tôi muốn quan điểm của tôi là một phần của scrollView)
===
Tôi cũng đã đọc trong hơn một nơi trực tuyến mà tôi sẽ không thể sử dụng cách tiếp cận Visual Format Language
để thiết lập các ràng buộc của tôi bởi vì những hạn chế nổi không được thiết lập tương đối so với SuperView ngay lập tức. Đây là đầu mối mà tôi đang làm điều gì đó sai ở trên vì phương pháp thứ 2 có thể sử dụng VCL trong khi đầu tiên sẽ cần phải tạo ra NSLayoutConstraint bằng tay.
===
Sau đây là mã Swift mà tôi đã thêm vào initialiser của lớp con NSScrollView tôi:
topCorner = NSButton()
topCorner.translatesAutoresizingMaskIntoConstraints = false
topCorner.bezelStyle = NSBezelStyle.CircularBezelStyle //.RecessedBezelStyle
topCorner.setButtonType(NSButtonType.PushOnPushOffButton)
topCorner.identifier = "topCorner"
topCorner.title = "TC"
self.addSubview(topCorner)
thc = NSLayoutConstraint(item: self.topCorner, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.superview, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0.0)
self.addConstraint(thc)
thc1 = NSLayoutConstraint(item: self.topCorner, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36.0)
self.addConstraint(thc1)
thc2 = NSLayoutConstraint(item: self.topCorner, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.superview, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0)
self.addConstraint(thc2)
thc3 = NSLayoutConstraint(item: self.topCorner, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 24.0)
self.addConstraint(thc3)
Chỉ cần để gây nhầm lẫn điều hơn nữa, thông báo lỗi rằng tôi nhận được chỉ là khi tôi có các ràng buộc thc và thc2 xung quanh trong mã. (Thử nghiệm với nhận xét về các ràng buộc khác nhau để xem nguyên nhân nào gây ra văn bản lỗi khủng khiếp.)