0

So I got a Window:

Window.xib

I got a WindowController too:

WindowController.h reads:

#import <Cocoa/Cocoa.h>

@interface MainWindowController : NSWindowController
{
    IBOutlet NSView *firstView;
    IBOutlet NSView *secondView;
    IBOutlet NSView *thirdView;

    int currentViewTag;
}

-(IBAction)switchView:(id)sender;


@end

And the WindowController.m reads:

#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

The problem I have is that when i switch tabs in my app, the custom view (and there are three of different sizes) draw differently each time. Look at the screenshots, all of the numbers should be centre aligned but they are sometimes and others not. Can anyone see what my error is please?

I will also add that all of the actions have been correctly configured + the code works perfectly if the custom view size is the same all the time.

The view that works

The view that almost works

Again pointing out that in my .xib all of the numbers are aligned to 0x and 0y axis.

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
Dmitri K.
  • 197
  • 1
  • 13

1 Answers1

-2

In Interface Builder, make sure to disable the "autoresizes subviews" checkbox of the default view of your window

Nonoche
  • 21
  • 1
  • 3