所以我有一个窗口:
Window.xib
我也有一个WindowController:
WindowController.h读取:
#import <Cocoa/Cocoa.h>
@interface MainWindowController : NSWindowController
{
IBOutlet NSView *firstView;
IBOutlet NSView *secondView;
IBOutlet NSView *thirdView;
int currentViewTag;
}
-(IBAction)switchView:(id)sender;
@end
WindowController.m读取:
#import "MainWindowController.h"
@interface MainWindowController ()
@end
@implementation MainWindowController
-(id)init
{
self = [super initWithWindowNibName:@"MainWindow"];
if (self){
// Initialization code here
}
return self;
}
//- (void)windowDidLoad {
// [super windowDidLoad];
//
// // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
//}
#pragma mark - Custom view drawing
-(NSRect)newFrameForNewContentView:(NSView *)view
{
NSWindow *window = [self window];
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;
}
-(NSView *)viewForTag:(int)tag{
NSView *view = nil;
if (tag == 0) {
view = firstView;
} else if (tag == 1) {
view = secondView;
} else {
view = thirdView;
}
return view;
}
-(BOOL) validateToolbarItem:(NSToolbarItem *)item
{
if ([item tag] == currentViewTag) return NO;
else return YES;
}
-(void)awakeFromNib
{
[[self window] setContentSize:[firstView frame].size];
[[[self window] contentView]addSubview:firstView];
[[[self window] contentView]setWantsLayer:YES];
}
-(IBAction)switchView:(id)sender
{
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 window]contentView]animator]replaceSubview:previousView with:view];
[[[self window]animator]setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
@end
我遇到的问题是,当我在我的应用中切换标签时,自定义视图(以及有三种不同尺寸)每次都会有不同的绘制方式。查看屏幕截图,所有数字应该居中对齐,但有时它们有些而其他数字则没有。任何人都能看到我的错误吗?
我还要补充说,所有操作都已正确配置+如果自定义视图大小始终相同,则代码可以正常工作。
再次指出在我的.xib中,所有数字都对齐到0x和0y轴。
Appdelegate.h
#import <Cocoa/Cocoa.h>
@class MainWindowController;
@interface AppDelegate : NSObject <NSApplicationDelegate> {
MainWindowController *mainWindowController;
}
@end
Appdelegate.m
@interface AppDelegate ()
@property (nonatomic) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
-(void)awakeFromNib
{
if(!mainWindowController){
mainWindowController = [[MainWindowController alloc]init];
}
[mainWindowController showWindow:nil];
}
@end
答案 0 :(得分:-2)
在Interface Builder中,确保禁用窗口默认视图的“自动调整子视图”复选框