以编程方式调整 NSView 的大小
我有一个主视图,应该显示几个子视图。这些子视图位于彼此的正上方和正下方(在 z 轴上),并且将向下(在 y 轴上)向下移动并使用以下代码向上移动:
if (!CGRectIsNull(rectIntersection)) {
CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
[backgroundView setFrame:newFrame];
} else{
[viewsUpdater invalidate];
viewsUpdater = nil;
}
rectIntersection 用于告知视图何时完全向下移动并且不再位于视图的后面前面的一个(当它们不再重叠 rectIntersection 为 null 时),它一次向下移动 2 个像素,因为这全部都在重复计时器内。我希望我的主视图(包含这两个其他视图的主视图)向下调整大小,以便它在背景视图降低时扩展。这是我正在尝试的代码:
CGRect mainViewFrame = [mainView frame];
if (!CGRectContainsRect(mainViewFrame, backgroundFrame)) {
CGRect newMainViewFrame = CGRectMake(0,
0,
mainViewFrame.size.width,
(mainViewFrame.size.height + 2));
[mainView setFrame:newMainViewFrame];
}
这个想法是检查 mainView 是否包含此背景视图。当backgroundView降低时,主视图不再包含它,并且它(应该)向下扩展2个像素。这种情况会发生,直到背景视图停止移动并且主视图最终包含背景视图。
问题是 mainView 根本没有调整大小。背景视图正在降低,我可以看到它,直到它从主视图的底部消失。 mainView 应该调整大小,但在任何方向上都没有改变。我尝试使用 setFrame 和 setBounds (带和不带 setNeedsDisplay),但没有任何效果。
我真的只是在寻找一种以编程方式更改主视图大小的方法。
I have a main view that is supposed to display several subviews. Those subviews are directly above and below one another (in the z axis) and will drop down (in the y axis) and move up using this code:
if (!CGRectIsNull(rectIntersection)) {
CGRect newFrame = CGRectOffset (rectIntersection, 0, -2);
[backgroundView setFrame:newFrame];
} else{
[viewsUpdater invalidate];
viewsUpdater = nil;
}
rectIntersection is used to tell when the view has entirely moved down and is no longer behind the front one (when they no longer overlap rectIntersection is null), it moves down by 2 pixels at a time because this is all inside a repeating timer. I want my main view, the one that contains these two other views, to resize downward so that it expands just as the view in the background is being lowered. This is the code I'm trying for that:
CGRect mainViewFrame = [mainView frame];
if (!CGRectContainsRect(mainViewFrame, backgroundFrame)) {
CGRect newMainViewFrame = CGRectMake(0,
0,
mainViewFrame.size.width,
(mainViewFrame.size.height + 2));
[mainView setFrame:newMainViewFrame];
}
The idea is to check if the mainView contains this background view. When the backgroundView is lowered, the main view no longer contains it, and it (should) expand downward by 2 pixels. This would happen until the background view stopped moving and the mainView finally contains backgroundView.
The problem is that the mainView is not resizing at all. the background view is being lowered, and I can see it until it disappears off the bottom of the mainView. mainView should have resized but it does not change in any direction. I tried using setFrame and setBounds (with and without setNeedsDisplay) but nothing worked.
I'm really just looking for a way to programmatically change the size of the main view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我明白了,问题是什么。我仔细阅读了代码。
rectIntersection
实际上是两个重叠视图的交集,并且当backgroundView
向下移动时,该矩形的高度会减小。这样
mainView
只调整一次大小。除此之外,这里有一个使用块语法的简单解决方案,为您的视图设置动画,此代码通常会在您的自定义视图控制器中执行。
I think I understood, what the problem is. I read carefuly the code.
rectIntersection
is actually the intersection of the two views, that overlap, and as thebackgroundView
is moved downward, that rect's height decreases.That way the
mainView
gets resized only one time.To add on this, here is a simple solution using block syntax, to animate your views, this code would typically take place in your custom view controller.