首先,我想说的是,我已经研究了许多其他资源来实现此目的,但是我所研究的似乎没有什么帮助。例如:Automatically grow document view of NSScrollView using auto layout?我有一个在界面生成器中创建的NSScrollView,并设置了约束来填充它所在的窗口:scrollview的Top,Left,Right和Bottom设置为等于Superview的Top,Left,Right和Bottom(是Xamarin中的viewcontroller使用的xib视图)。我想要的文档视图是一个简单的NSView,它在ViewController的ViewDidLoad中被NSImageViews动态填充:EventListScrollView.TranslatesAutoresizingMaskIntoConstraints = false;var documentView = new NSView(EventListScrollView.Bounds);//documentView.AutoresizingMask = NSViewResizingMask.WidthSizable | NSViewResizingMask.HeightSizable;//documentView.TranslatesAutoresizingMaskIntoConstraints = false;NSView lastHeader = null;foreach(var project in _projects){ var imageView = new NSImageView(); imageView.TranslatesAutoresizingMaskIntoConstraints = false; imageView.SetContentCompressionResistancePriority(1, NSLayoutConstraintOrientation.Horizontal); imageView.SetContentCompressionResistancePriority(1, NSLayoutConstraintOrientation.Vertical); imageView.ImageScaling = NSImageScale.ProportionallyUpOrDown; var xPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Left, 1, 0); NSLayoutConstraint yPosConstraint = null; if(lastHeader!=null) { yPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, lastHeader, NSLayoutAttribute.Bottom, 1, 0); } else { yPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Top, 1, 0); } var widthConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Width, 1, 0); var heightConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, imageView, NSLayoutAttribute.Width, (nfloat)0.325, 0); documentView.AddSubview(imageView); documentView.AddConstraints(new[] { xPosConstraint, yPosConstraint, widthConstraint, heightConstraint }); lastHeader = imageView; ImageService.Instance.LoadUrl($"https:{project.ProjectLogo}").Into(imageView);}EventListScrollView.DocumentView = documentView;由于我不一定知道_projects中有多少个项目,也不知道要加载到每个imageView中的图像的高度,尽管我知道预期的比率,但我不知道直到运行时我documentView的高度。我希望每次调整滚动视图所在的封闭窗口(并因此也改变滚动视图)的大小时,将documentView的宽度调整为与NSClipView相同。至于documentView的高度,我希望它由我在imageView中填充的documentView的大小确定,这就是为什么高度约束相对于宽度的原因。我玩过AutoresizingMask的documentView,NSClipView,scrollview等。我尝试将所有内容的TranslatesAutoresizingMaskIntoConstraints设置为false并添加约束以设置Left,Right和Top documentView的值等于NSClipView的值。当我这样做时,该视图甚至似乎都没有显示出来。我似乎无法弄清楚这一点。我也尝试过不使用NSImageViews来执行此操作:public override void ViewDidLoad(){ base.ViewDidLoad(); EventListScrollView.TranslatesAutoresizingMaskIntoConstraints = false; var clipView = new NSClipView { TranslatesAutoresizingMaskIntoConstraints = false }; EventListScrollView.ContentView = clipView; EventListScrollView.AddConstraint(NSLayoutConstraint.Create(clipView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, EventListScrollView, NSLayoutAttribute.Left, 1, 0)); EventListScrollView.AddConstraint(NSLayoutConstraint.Create(clipView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, EventListScrollView, NSLayoutAttribute.Right, 1, 0)); EventListScrollView.AddConstraint(NSLayoutConstraint.Create(clipView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, EventListScrollView, NSLayoutAttribute.Top, 1, 0)); EventListScrollView.AddConstraint(NSLayoutConstraint.Create(clipView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, EventListScrollView, NSLayoutAttribute.Bottom, 1, 0)); var documentView = new NSView(); documentView.WantsLayer = true; documentView.Layer.BackgroundColor = NSColor.Black.CGColor; documentView.TranslatesAutoresizingMaskIntoConstraints = false; EventListScrollView.DocumentView = documentView; clipView.AddConstraint(NSLayoutConstraint.Create(documentView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Left, 1, 0)); clipView.AddConstraint(NSLayoutConstraint.Create(documentView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Right, 1, 0)); clipView.AddConstraint(NSLayoutConstraint.Create(documentView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Top, 1, 0)); NSView lastHeader = null; foreach(var project in _projects) { var random = new Random(); var imageView = new NSView(); imageView.TranslatesAutoresizingMaskIntoConstraints = false; imageView.WantsLayer = true; imageView.Layer.BackgroundColor = NSColor.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)).CGColor; var xPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Left, 1, 0); NSLayoutConstraint yPosConstraint = null; if(lastHeader!=null) { yPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, lastHeader, NSLayoutAttribute.Bottom, 1, 0); } else { yPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Top, 1, 0); } var widthConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Width, 1, 0); var heightConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, imageView, NSLayoutAttribute.Width, (nfloat)0.325, 0); documentView.AddSubview(imageView); documentView.AddConstraints(new[] { xPosConstraint, yPosConstraint, widthConstraint, heightConstraint }); lastHeader = imageView; }} 最佳答案 我想到了。因此,诀窍是除非将documentView的底部固定到子视图的最后一个,否则documentView不会知道其大小。您可以使用视觉格式来执行此操作,但是如果您直到运行时才真正知道视图是什么,那一定可以解决。这是对我有用的代码:public override void ViewDidLoad(){ base.ViewDidLoad(); var clipView = EventListScrollView.ContentView; var documentView = new FlippedView(); documentView.TranslatesAutoresizingMaskIntoConstraints = false; EventListScrollView.DocumentView = documentView; clipView.AddConstraint(NSLayoutConstraint.Create(documentView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Left, 1, 0)); clipView.AddConstraint(NSLayoutConstraint.Create(documentView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Right, 1, 0)); clipView.AddConstraint(NSLayoutConstraint.Create(documentView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, clipView, NSLayoutAttribute.Top, 1, 0)); NSView lastHeader = null; foreach (var project in _projects) { var imageView = new NSImageView(); imageView.TranslatesAutoresizingMaskIntoConstraints = false; imageView.SetContentCompressionResistancePriority(1, NSLayoutConstraintOrientation.Horizontal); imageView.SetContentCompressionResistancePriority(1, NSLayoutConstraintOrientation.Vertical); imageView.ImageScaling = NSImageScale.ProportionallyUpOrDown; var xPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Left, 1, 0); NSLayoutConstraint yPosConstraint = null; if (lastHeader != null) { yPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, lastHeader, NSLayoutAttribute.Bottom, 1, 0); } else { yPosConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Top, 1, 0); } var widthConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, documentView, NSLayoutAttribute.Width, 1, 0); var heightConstraint = NSLayoutConstraint.Create(imageView, NSLayoutAttribute.Height, NSLayoutRelation.LessThanOrEqual, imageView, NSLayoutAttribute.Width, (nfloat)0.360, 0); documentView.AddSubview(imageView); documentView.AddConstraint(xPosConstraint); documentView.AddConstraint(yPosConstraint); documentView.AddConstraint(widthConstraint); documentView.AddConstraint(heightConstraint); lastHeader = imageView; ImageService.Instance.LoadUrl($"https:{project.ProjectLogo}").Into(imageView); } if(lastHeader!=null) { var bottomPinConstraint = NSLayoutConstraint.Create(documentView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, lastHeader, NSLayoutAttribute.Bottom, 1, 0); documentView.AddConstraint(bottomPinConstraint); }}关键是最后一个约束-bottomPinConstraint。这允许documentView扩展其高度以适合所有子视图。另外,即使我要手动设置IsFlipped = true;和NSClipView之间的约束,我仍然需要使用documentView视图,否则它将忽略这些约束并将视图固定在。我的EventListScrollView是在Interface Builder中创建的,并且设置了约束以使其填充其中的整个窗口。最终结果是一个带有DocumentView的NSScrollView,它填充了ScrollView的宽度,但也垂直扩展以适合我以编程方式添加到其中的子视图。
10-06 12:48