如何从 UITabBarButton 检索 CGRect 框架?
Posted
技术标签:
【中文标题】如何从 UITabBarButton 检索 CGRect 框架?【英文标题】:How to retrieve CGRect frame from UITabBarButton? 【发布时间】:2014-02-15 20:31:18 【问题描述】:我的目标是在我的教程中启动应用程序时为我的每个 UITabBarbuttons 显示一个 UIPopover...我正在执行以下操作:
如何从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,并且弹出框处于完美位置。但我宁愿通过检索值来进行计算...那么如何提取上述结果的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;
提前谢谢...
-保罗斯
【问题讨论】:
【参考方案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;
等等
【讨论】:
非常感谢您的详细解释。不胜感激。 很高兴我能帮上忙。 :)以上是关于如何从 UITabBarButton 检索 CGRect 框架?的主要内容,如果未能解决你的问题,请参考以下文章
iOS在UITabBar中更改UITabBarButton的框架