簡體   English   中英

子類化UIWindow

[英]Subclassing UIWindow

我只是試圖將UIWindow子類化,以便可以攔截一些通知。 除了下面列出的代碼,我還進入MainWindow.xib並將UIWindow對象更新為我的子類。 它可以很好地加載,問題是我的標簽欄上的標簽沒有響應(在下面的示例中,我僅添加了一個標簽,但是在我的應用中,我有多個標簽(不是問題))。 誰能看到我可能做錯了什么? 謝謝。

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

進口

@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;
}

問題是我包括了UIWindows-(void)sendEvent:(UIEvent *)event方法,但是沒有對其調用super。 我打電話給它,一切都固定了。

編輯:請注意,這可以回答原始問題,在進行重大重寫之前,可以更改整個問題

您首先應該找出是否有記錄的方式(某些調用或重寫的方法)來更改導航欄的高度。 我非常懷疑有一個。 而且我也懷疑UIWindow是否是尋找它的地方-導航欄是UINavigationController的一部分。

假設沒有合法的方法強制高度保持為44px,則可以嘗試以下幾種方法:

  1. (不建議使用)破壞UINavigationController的整個自動旋轉機制,自己處理視圖控制器的旋轉,例如從UIWindow開始,就像您現在開始做的那樣。 UINavigationController將僅“感覺”調整大小,並且不知道它實際上在旋轉。 UINavigationController不得旋轉,因此-shouldAutoRotate.. { return NO; } -shouldAutoRotate.. { return NO; } 快速檢查一下,如果將最頂層VC的幀簡單地設置為CGRectMake(0,0,480,320);會發生什么情況CGRectMake(0,0,480,320); (從您的子類UINavigationController或從子類UIWindow)。 如果一切正常,那么您只需添加自動旋轉部分。
  2. (推薦)不要依賴UINavigationController繪制導航欄,而是自己做。 這樣做沒有什么樂趣,但是有99%的保證可以使它正常工作,並且不會在每次更新iOS時中斷。
  3. (中性,快速和骯臟修復)為橫向模式,將您自己的UINavigationBar放在常規視圖的頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM