我的目标是在我的教程中启动应用程序时,为每个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;
提前致谢...
-PaulS
答案 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;
等