使用工具栏在自定义视图之间切换

时间:2013-06-23 13:31:13

标签: objective-c xcode cocoa

我有一个MainMenu.xib文件,我有一个名为“Preferences”的窗口 此外,我有四个不同高度的不同自定义视图。和该窗口上的工具栏。 我已将每个工具栏项链接到 - (void)switchViews:(id)sender;功能。视图已链接并且正在显示并且也在发生变化,但问题是如果我从一个视图更改为另一个视图然后返回或另一个视图,则视图上的按钮,标签,复选框等内容会继续向上移动,窗口仍然是继续改变到正确的高度,但如果我切换足够的时间,内容将从视图中消失..任何类型的帮助将是有用的。

http://imgur.com/bV3NHa0,zgJCacA http://imgur.com/bV3NHa0,zgJCacA#1

// FOR CHANGING VIEWS IN THE PREFERENCES WINDOW
- (IBAction)switchView:(id)sender {

long int tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag:currentViewTag];
currentViewTag = tag;

NSRect newFrame = [self newFrameForNewContentView:view];

[NSAnimationContext beginGrouping];

if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) {
    [[NSAnimationContext currentContext] setDuration:1.0];
}

[[[[self preferences] contentView] animator] replaceSubview:previousView with:view];
[[[self preferences] animator] setFrame:newFrame display:YES];

[NSAnimationContext endGrouping];

}

- (NSView *)viewForTag:(long int)tag {

  NSView *view = nil;
  switch (tag) {
    case 0:
        view = generalView;
        break;
    case 1:
        view = timesView;
        break;
    case 2:
        view = menubarView;
        break;
    case 3: default: view = aboutView;
        break;
  }
  return  view;
}

- (NSRect)newFrameForNewContentView:(NSView *)view {

NSWindow *window = [self preferences];
NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;

NSRect frame = [window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);

return frame;
}

1 个答案:

答案 0 :(得分:0)

如果您正在描述和浏览代码,我会开始怀疑这条线......

frame.origin.y -= (newSize.height - oldSize.height);