简体   繁体   English

改变Cocoa的窗口大小?

[英]Change size of window in Cocoa?

I have a window whose size I need to change when the user clicks on it. 我有一个窗口,当用户点击它时我需要更改它的大小。 I am using [self setFrame:windowFrame display:YES animate:YES] to accomplish this. 我正在使用[self setFrame:windowFrame display:YES animate:YES]来完成此任务。

Even though the window successfully changes size (I increase its height), it moves the contents of the window up with it. 即使窗口成功地改变了大小(我增加了它的高度),它也会向上移动窗口的内容。 How do I prevent this from happening? 我该如何防止这种情况发生? I want the contents to remain in place. 我希望内容保持不变。

I am on OSX Mountain Lion developing an app for OSX using Objective-C and Cocoa. 我在OSX Mountain Lion上使用Objective-C和Cocoa为OSX开发应用程序。

EDIT: Constraints and/or Springs and Struts will not work as I need to move the contents around after the window is resized. 编辑:约束和/或弹簧和Struts将无法工作,因为我需要在调整窗口大小后移动内容。

If you are using the Interface Builder to build these views, then I believe one approach is to set the "struts and springs." 如果您使用Interface Builder来构建这些视图,那么我认为一种方法是设置“struts和spring”。 These are available under the "size inspector" and are the red arrows and bars above the "autosizing" label. 这些在“尺寸检查器”下可用,并且是“自动调整”标签上方的红色箭头和条形。 Play around with these to get the effect that you want, but the general idea is that the arrows control how the size of the view adjusts to changes in the size of the parent view, and the bars control the relationship of the edges of the view to the edges of the parent view as the size changes. 玩这些以获得您想要的效果,但一般的想法是箭头控制视图的大小如何调整父视图的大小的变化,并且条控制视图的边缘的关系随着大小的变化,到父视图的边缘。

Constraints and/or Springs and Struts will not work as I need to move the contents around after the window is resized. 约束和/或Springs和Struts不起作用,因为我需要在调整窗口大小后移动内容。

In that case, you should use NSViewAnimation . 在这种情况下,您应该使用NSViewAnimation

A single view animation can actually perform multiple animations to multiple views, and you can even do one to a window, despite the class's name and the fact that windows aren't views in Cocoa. 单个视图动画实际上可以对多个视图执行多个动画,您甚至可以对窗口执行一个动画,尽管类的名称和窗口不是Cocoa中的视图这一事实。

You create a view animation with initWithViewAnimations: , which takes an array of dictionaries . 使用initWithViewAnimations:创建一个视图动画,它采用一系列字典 Each dictionary identifies the target ( NSViewAnimationTargetKey ) and what to do to it: Either change the target's frame ( NSViewAnimationStartFrameKey and NSViewAnimationEndFrameKey ) or fade the target in or out ( NSViewAnimationEffectKey ). 每个字典标识目标( NSViewAnimationTargetKey )以及如何处理它:更改目标的帧( NSViewAnimationStartFrameKeyNSViewAnimationEndFrameKey )或淡入或淡出目标( NSViewAnimationEffectKey )。 For your case, you'll be changing the targets' frames. 对于您的情况,您将更改目标的框架。

When the user does the thing that causes the resize of the window, you'll need to compute the desired overall size of the window (taking care to adjust its frame's position so it doesn't grow off the screen), as well as the new frames—both positions and sizes—of your views. 当用户执行导致窗口大小调整的事物时,您需要计算窗口所需的整体大小(注意调整其框架的位置,使其不会在屏幕上生长),以及新视图 - 视图的位置和大小。 Everything that will move and/or change size, create a dictionary for it and throw it into the array. 将移动和/或改变大小的所有内容,为其创建字典并将其放入数组中。 Then create the view animation. 然后创建视图动画。

An NSViewAnimation is a kind of NSAnimation , which provides all the methods for starting and stopping the animation, monitoring its progress, hooking into it, and chaining multiple NSAnimations together. NSViewAnimation是一种NSAnimation ,它提供了启动和停止动画,监视其进度,挂钩动画以及将多个NSAnimations链接在一起的所有方法。 If nothing else, you'll need to start the animation . 如果没有别的,你需要开始动画

In constraint-based layout, set the views around the edge of your window to be a fixed distance from their superview's edge. 在基于约束的布局中,将窗口边缘周围的视图设置为与其超视图边缘的固定距离。

Xcode will infer a lot of resizability from that; Xcode将从中推断出很多可恢复性; if anything still isn't resizing properly, adjust its constraints so that its width and/or height is no longer constant. 如果仍然没有正确调整大小,请调整其约束,使其宽度和/或高度不再恒定。

The easiest way is to move your views until blue lines show up in the editor. 最简单的方法是移动视图,直到编辑器中出现蓝线。 Each blue line corresponds to a rule in the HIG about how things should be lain out, and if you drop the view there, Xcode will create constraints matching those guidelines. 每条蓝线对应于HIG中关于事物应该如何显示的规则,如果您将视图放在那里,Xcode将创建符合这些准则的约束。 For example, if you set a view 20 points from the right edge of its superview, you'll get a blue line for that, and if you drop the view there, you'll create a constraint that the view must remain that distance from that edge. 例如,如果您将视图从其超级视图的右边缘设置了20个点,那么您将获得一条蓝线,如果您将视图放在那里,您将创建一个约束,该视图必须保持该视图的距离那边缘。

The superview isn't the only view with which you can create HIG-based constraints. superview不是唯一可以创建基于HIG的约束的视图。 You can also create guideline constraints between sibling views. 您还可以在兄弟视图之间创建指南约束。 For example, if you put a button next to another button at the appropriate distance, you'll get a blue line across that distance, and if you drop it, you'll create a constraint that those two buttons must remain that distance from each other. 例如,如果您将一个按钮放在另一个按钮旁边的适当距离处,那么您将在该距离上获得一条蓝线,如果您放下它,您将创建一个约束,即这两个按钮必须与每个按钮保持距离。其他。

If you want to do something really custom, the three buttons in the lower-right corner of the nib editor will let you create any constraint you want. 如果你想做一些真正自定义的东西,nib编辑器右下角的三个按钮可以让你创建你想要的任何约束。 What you have selected determines what constraints you can create; 您选择的内容决定了您可以创建的约束; the nib editor's outline view will help you make sure you have the selection you want. 笔尖编辑器的大纲视图将帮助您确保获得所需的选择。

You are going to have to iterate through all of your subviews and change their frame positions based on the delta of your window frame. 您将不得不遍历所有子视图并根据窗口框架的增量更改其框架位置。

so if you expand your window frame by 20 in all directions, all your subviews are going to have to increase their frame positions by (20,20) to offset the windows movement. 因此,如果您在所有方向上将窗口框架扩展20,则所有子视图都必须将其框架位置增加(20,20)以抵消窗口移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM