可可动画框架在偏好窗口中

Mat*_*oal 3 cocoa

查看许多OSX应用程序,我经常看到首选项窗口框架根据工具栏按钮激活的视图内容增长和减少.

我想知道是否有一种方法可以自动化帧大小维度,或者唯一的方法是以编程方式调整大小和动画.

iai*_*ain 7

我想你只能使用NSWindow的setFrame:display:animate:我不认为窗口可以自动调整大小.因此,当您更改内容时,您可以执行以下操作:

NSRect oldContentFrame = [oldContentView frame];
NSRect newContentFrame = [newContentView frame];

float widthDifference = oldContentFrame.size.width - newContentFrame.size.width;
float heightDifference = oldContentFrame.size.height - newContentFrame.size.height;

// Change the size of the window by the difference between the two views
// and move the frame up/down
NSRect windowFrame = [window frame];
windowFrame.size.width -= widthDifference;
windowFrame.size.height -= heightDifference;
windowFrame.origin.y += heightDifference;

// Remove the old content
[oldContentView removeFromSuperview];

// Change the size
[window setFrame:windowFrame display:YES animate:YES];

// Add the new view
[window setContentView:newContentView];
Run Code Online (Sandbox Code Playgroud)