Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing UIWindow

I am simply trying to subclass UIWindow so that I can intercept some notifications. Along with the code listed below I also go into MainWindow.xib and update the UIWindow object to my sub class. It loads up fine, problem is the tabs on my tab bar are unresponsive (in the example below I only added one tab, but in my app I have multiple (that's not the problem)). Can anyone see what I might be doing wrong? Thanks.

UISubclassedWindow.h

#import <UIKit/UIKit.h>

@interface UISubclassedWindow : UIWindow 
{

}

@end

UISubclassedWindow.m

    #import "UISubclassedWindow.h"

    @implementation UISubclassedWindow

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        NSLog(@"init");
    }
    return self;
}

    - (void)makeKeyAndVisible
    {
        [super makeKeyAndVisible];
        NSLog(@"makeKeyAndVisible");
    }

    - (void)becomeKeyWindow
    {
        [super becomeKeyWindow];
        NSLog(@"becomeKeyWindow");

    }

    - (void)makeKeyWindow
    {
        [super makeKeyWindow];
        NSLog(@"makekeyWindow");
    }

    - (void)sendEvent:(UIEvent *)event
    {
    }

    - (void)dealloc 
    {
        [super dealloc];
    }

    @end

AppDelegate.h

import

@class UISubclassedWindow;

@interface My_AppAppDelegate : NSObject <UIApplicationDelegate> 
{
    UISubclassedWindow *window;
}

@property (nonatomic, retain) IBOutlet UISubclassedWindow *window;

@end

AppDelegate.m

@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    MainViewController *mainViewController = [[MainViewController alloc] initWithViewType: 0];
    UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController];
    mainNavigationController.title = @"Main";
    [[mainNavigationController navigationBar] setBarStyle: UIBarStyleBlack];

    [tabBarController setViewControllers: [NSArray arrayWithObjects: mainNavigationController,  nil]];

    [self.window setRootViewController: tabBarController];
    [self.window makeKeyAndVisible];

    [mainViewController release];
    [mainNavigationController release];
    [tabBarController release];

    return YES;
}
like image 700
Ser Pounce Avatar asked Jan 13 '12 00:01

Ser Pounce


People also ask

What is Uiwindow in Swift?

The backdrop for your app's user interface and the object that dispatches events to your views.

Can we have multiple Uiwindows in IOS application?

Yes, you can have multiple windows. A key window is the one who receives the user input.

What is Uiscene?

An object that represents one instance of your app's user interface.

What is Makekeyandvisible?

This is a convenience method to show the current window and position it in front of all other windows at the same level or lower. If you only want to show the window, change its isHidden property to false .


2 Answers

The problem was I was including the UIWindows - (void)sendEvent:(UIEvent *)event method, but wasn't calling super on it. I called super on it and everything was fixed.

like image 177
Ser Pounce Avatar answered Oct 27 '22 23:10

Ser Pounce


edit: note that this answers the original question, before a major rewrite changing the entire issue

You should first find out whether there is a documented way (some call, or an overridden method) to change the height of the nav bar. I highly doubt there is one. And I also doubt if UIWindow is the place to look for it - the nav bar is part of the UINavigationController.

Assuming there is no legal way to force the height to remain 44px, you can try several things:

  1. (not recommended) break the entire autorotation machinery of UINavigationController, handle the rotation of the view controllers yourself, e.g. from UIWindow, like you are starting to do now. The UINavigationController will only "feel" a resize and doesn't know it is actually rotating. The UINavigationController must not do rotations, so -shouldAutoRotate.. { return NO; }. As a quick check, see what happens if you would simply set the frame of the topmost VC to CGRectMake(0,0,480,320); (either from your subclassed UINavigationController or from the subclassed UIWindow). If everything still works OK, then you only need to add the autorotation part.
  2. (recommended) don't rely on UINavigationController for drawing navigation bars, but do it all yourself. Not much fun to do, but a 99% guarantee that you will get it working and it will not break on every update of iOS.
  3. (neutral, quick&dirty fix) for landscape mode, put your own UINavigationBar on top of the regular one.
like image 41
mvds Avatar answered Oct 27 '22 21:10

mvds