例如:Automatically grow document view of NSScrollView using auto layout?
我有一个我在Interface Builder中创建的NSScrollVIEw并设置约束以填充它所在的窗口:滚动视图的顶部,左侧,右侧和底部设置为等于超级视图的顶部,右侧和底部(其中是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,1,0); NSLayoutConstraint yPosConstraint = null; if(lastheader!=null) { yPosConstraint = NSLayoutConstraint.Create(imageVIEw,NSLayoutAttribute.top,lastheader,NSLayoutAttribute.Bottom,0); } else { yPosConstraint = NSLayoutConstraint.Create(imageVIEw,0); } var wIDthConstraint = NSLayoutConstraint.Create(imageVIEw,NSLayoutAttribute.WIDth,0); var heightConstraint = NSLayoutConstraint.Create(imageVIEw,NSLayoutAttribute.Height,imageVIEw,(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的宽度都会调整为与NSClipVIEw相同.至于documentVIEw的高度,我希望它由我在documentVIEw中填充的imageVIEws的大小决定,这就是高度约束相对于宽度的原因.
我已经使用了documentVIEw的autoresizingMask,NSClipVIEw,scrollvIEw等.我尝试将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,EventListScrollVIEw,0)); EventListScrollVIEw.AddConstraint(NSLayoutConstraint.Create(clipVIEw,NSLayoutAttribute.Right,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,clipVIEw,0)); clipVIEw.AddConstraint(NSLayoutConstraint.Create(documentVIEw,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)).CGcolor; var xPosConstraint = NSLayoutConstraint.Create(imageVIEw,0); NSLayoutConstraint yPosConstraint = null; if(lastheader!=null) { yPosConstraint = NSLayoutConstraint.Create(imageVIEw,0); } else { yPosConstraint = NSLayoutConstraint.Create(imageVIEw,0); } var wIDthConstraint = NSLayoutConstraint.Create(imageVIEw,0); var heightConstraint = NSLayoutConstraint.Create(imageVIEw,0); documentVIEw.AddSubvIEw(imageVIEw); documentVIEw.AddConstraints(new[] { xPosConstraint,heightConstraint }); lastheader = imageVIEw; }}解决方法 我想到了.所以,这样做的诀窍在于,除非你将底部固定在最后一个子视图中,否则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,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,0); NSLayoutConstraint yPosConstraint = null; if (lastheader != null) { yPosConstraint = NSLayoutConstraint.Create(imageVIEw,NSLayoutRelation.LessthanorEqual,(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,0); documentVIEw.AddConstraint(bottomPinConstraint); }}
关键是最后一个约束 – bottomPinConstraint.这允许documentVIEw扩展其高度以适应所有子视图.
另外,我仍然需要使用IsFlipped = true的视图;即使我手动设置NSClipVIEw和我的documentVIEw之间的约束,否则它会忽略约束并将我的视图固定到NSClipVIEw的底部.我的EventListScrollVIEw是在Interface Builder中创建的,并且设置了约束以使其填充整个窗口.
最终结果是一个带有documentVIEw的NSScrollVIEw,它填充了ScrollVIEw的宽度,但也垂直扩展以适应我以编程方式添加到它的子视图.
总结以上是内存溢出为你收集整理的macos – 具有匹配宽度DocumentView的NSScrollView全部内容,希望文章能够帮你解决macos – 具有匹配宽度DocumentView的NSScrollView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)