gpt4 book ai didi

objective-c - 滚动 NSScrollView 同时尝试保留 subview 的位置

转载 作者:行者123 更新时间:2023-12-03 17:39:20 25 4
gpt4 key购买 nike

我有一个 NSScrollView 的子类。在该 ScrollView 的文档 View 中,我有几个 NSView。最后,我将一个名为 aView 的 NSView 添加到 documentView 中。

只要不需要滚动,我希望 aView 位于 documentView 的底部。只能沿 y 轴滚动。

如果 documentView 对于 contentView 来说太高 - 因此需要滚动 - 我希望 aView 显示在 contentView 的底部。

这与您在下面看到的代码配合得很好。

这是我的问题:当我开始滚动时,我希望 aView 停留在 contentView 的底部,但 aView 只是与 documentView 中的所有其他 View 一起滚动。换句话说:如果需要滚动,我希望将 aView 的位置固定在 ScrollView 可见矩形的底部,如果 contentView 足够高以显示,则将其固定在 documentView 的底部整个文档 View 。

有办法做到这一点吗?我哪里出错了?

这是我的子类 NSScrollView 中的代码:

    [documentView addSubview:aView];

aView.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *documentAViewBottom = [NSLayoutConstraint constraintWithItem:documentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:aView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
documentAViewBottom.priority = NSLayoutPriorityDefaultHigh;
[documentView addConstraint:documentAViewBottom];

NSLayoutConstraint *aViewMaxYEdge = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:aView attribute:NSLayoutAttributeBottom multiplier:1 constant:5];
[self addConstraint:aViewMaxYEdge];

[documentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[aView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(aView)]];

最佳答案

我有点老派, NSLayoutConstraints 对我没有吸引力,所以这里是一个替代的建议手动解决方案

当您设置文档 View 时,从 NSScrollView 的子类中,

[self.contentView setPostsBoundsChangedNotification:YES];

然后订阅更改的边界

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentViewDidScroll:)
name:NSViewBoundsDidChangeNotification object:self.contentView];

然后在

-(void)contentViewDidScroll
{
double widthOfAView = aView.frame.size.width;
double heightOfAView = aView.frame.size.height;
[aView setFrameOrigin:NSMakePoint(NSMinX(self.contentView.bounds) - widthOfAView, NSMaxY(self.contentView.bounds) - heightOfAView)];
}

当然,所有这些都假设您的 isFlipped 被覆盖为 YES。

关于objective-c - 滚动 NSScrollView 同时尝试保留 subview 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23135821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com