2014-02-15 25 views
0

我的目標是當我的教程的一部分,啓動應用程序......我做以下,以顯示我的每個UITabBarbuttons的UIPopover:如何從UITabBarButton檢索CGRect框架?

如何提取幀從UIView *tabView = [[appDelegate tabBarMutableArray] objectAtIndex:1];這是第一個UITabBarButton。

Array AppDelegate: <UITabBarButton: 0x145d06a70; frame = (254 1; 76 55); opaque = NO; layer = <CALayer: 0x17802c160>> 

現在我正在手動輸入254,712,76,55的GCRect。通過減去768-56,我得到了712,而且這個popover處於完美的位置。但我寧願通過取回數值來進行計算......所以我如何提取frame = (254 1; 76 55)以上結果的一部分?

frame = CGRectMake(254,712,76,55); 

AppDelegate.h

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 

- (NSMutableArray *)tabBarMutableArray; 

@end 

AppDelegate.m

#import "AppDelegate.h" 

@implementation AppDelegate 
#define debug 1 

- (NSMutableArray *)tabBarMutableArray; 
{ 
    if (debug==1) { 
     NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd)); 
    } 

    UITabBarController *tabController   = (UITabBarController *)self.window.rootViewController; 
    NSMutableArray  *tabBarItemsMutableArray = [NSMutableArray new]; 

    UITabBar *tabBar = tabController.tabBar; 

    for (UIView *view in tabBar.subviews) 
    { 
     [tabBarItemsMutableArray addObject:view.description]; 
    } 

    return tabBarItemsMutableArray; 
} 

viewcontroller.m

-(void)showHomeTabBarPopOver 
{ 
    if (debug==1) { 
     NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd)); 
    } 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    CGRect frame = CGRectZero; 

    NSLog(@"%@", [appDelegate tabBarMutableArray]); 

    NSLog(@"Array AppDelegate: %@", [[appDelegate tabBarMutableArray] objectAtIndex:1]); 


    UIView *tabView = [[appDelegate tabBarMutableArray] objectAtIndex:1]; 
    NSLog(@"tabView %@", [tabView valueForKey:@"frame"]); 

    // frame   = tabView.frame; 
    NSLog(@"frame %@", CGRectCreateDictionaryRepresentation(self.view.frame)); 

    frame = CGRectMake(254,712,76,55); //254,712,76,55 

    [_getStartedPopover presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

} 

結果

2014-02-07 14:35:47.940 mCC HD[1964:60b] (
    "<_UITabBarBackgroundView: 0x145d0cb30; frame = (0 0; 1024 56); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x17802db00>>", 
    "<UITabBarButton: 0x145d06a70; frame = (254 1; 76 55); opaque = NO; layer = <CALayer: 0x17802c160>>", 
    "<UITabBarButton: 0x145e2e360; frame = (364 1; 76 55); opaque = NO; layer = <CALayer: 0x17003c2e0>>", 
    "<UITabBarButton: 0x145d0a060; frame = (474 1; 76 55); opaque = NO; layer = <CALayer: 0x17802c6a0>>", 
    "<UITabBarButton: 0x145d0a640; frame = (584 1; 76 55); opaque = NO; layer = <CALayer: 0x17802ca20>>", 
    "<UITabBarButton: 0x145d0ae60; frame = (694 1; 76 55); opaque = NO; layer = <CALayer: 0x17802cda0>>", 
    "<UIImageView: 0x145d0edd0; frame = (0 -0.5; 1024 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x17802f0a0>>" 
) 
2014-02-07 14:35:47.940 mCC HD[1964:60b] Running AppDelegate 'tabBarMutableArray' 
2014-02-07 14:35:47.941 mCC HD[1964:60b] Array AppDelegate: <UITabBarButton: 0x145d06a70; frame = (254 1; 76 55); opaque = NO; layer = <CALayer: 0x17802c160>> 
2014-02-07 14:35:47.943 mCC HD[1964:60b] frame { 
    Height = 768; 
    Width = 1024; 
    X = 0; 
    Y = 0; 

在此先感謝...

-PaulS

回答

1

編輯:

(對不起,我的第一個答案,我沒看如何創建tabBarMutableArray。)


在你的AppDelegate中tabBarItemsMutableArray(這是您的-tabBarMutableArray方法的返回值)被創建這樣的:

for (UIView *view in tabBar.subviews) 
{ 
    [tabBarItemsMutableArray addObject:view.description]; 
} 

因此有包含在該陣列中沒有UIView對象,但NSString對象(全部子視圖的描述)。這就是爲什麼在你的viewcontroller.m以下行不正確,並根據需要將無法正常工作:

UIView *tabView = [[appDelegate tabBarMutableArray] objectAtIndex:1]; 

該行的右手邊是一個NSString,左手邊是一個UIView


可以解決這個問題,加入行

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];   
UITabBarController *tabController = (UITabBarController *)appDelegate.window.rootViewController; 
UITabBar *tabBar = tabController.tabBar; 
UIView *tabView = [tabBar.subviews objectAtIndex:1]; 
CGRect frame = tabView.frame 

-showHomeTabBarPopOver方法。 (雖然有這取決於你想要做什麼......整潔的方法)如果你需要的所有標籤欄項目框架中,並不僅這個特殊的


「索引:1」我建議你創建您的AppDelegate中的一個屬性。H:

@property (strong, nonatomic) NSArray *tabBarViews; 

,實施您的AppDelegate.m其getter方法:

- (NSArray *)tabBarViews { 
    UITabBarController *tabController = (UITabBarController *)self.window.rootViewController; 
    UITabBar *tabBar = tabController.tabBar; 

    return tabBar.subviews; 
} 

然後你可以從你的viewcontroller.m包括其框架內更方便地訪問你的標籤欄的意見

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

UIView *tabView0 = [appDelegate.tabBarViews objectAtIndex:0]; 
CGRect frame0 = tabView0.frame; 

UIView *tabView1 = [appDelegate.tabBarViews objectAtIndex:1]; 
CGRect frame1 = tabView1.frame; 

+1

非常感謝您的詳細解釋。非常感謝。 –

+0

很高興我能幫上忙。 :) – Mischa