首先,我想说的是,我已经研究了许多其他资源来实现这一目标,但是我所研究的似乎都没有帮助。
例如:Automatically grow document view of NSScrollView using auto layout?
我有一个NSScrollView,它是在Interface Builder中创建的,并设置了约束来填充它所在的窗口:scrollview的Top,Left,Right和Bottom设置为等于它的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
,滚动视图等。我尝试将TranslatesAutoresizingMaskIntoConstraints
设置为false
一切,并添加约束以将documentView
的Left,Right和Top设置为与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;
}
}
答案 0 :(得分:1)
我知道了。因此,诀窍是除非将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
的视图,否则它将忽略这些约束并且将我的视图固定在NSClipView
的底部。我的EventListScrollView是在Interface Builder中创建的,并且设置了约束以使其填充其中的整个窗口。
最终结果是一个带有DocumentView的NSScrollView,它填充了ScrollView的宽度,但也可以垂直扩展以适合我以编程方式添加到其中的子视图。
答案 1 :(得分:0)
我这样做是为了使我的自定义视图能够按滚动视图的宽度进行调整。
pepe
自定义视图在X轴上提供manolo
。