1

我正在开发一个基于 tabbarcontroller 的应用程序。在一个视图控制器中,我想隐藏 tabbarController 而不是我想为此显示工具栏,我编写了以下代码

    //tabBAr hidden
    [self.tabBarController.tabBar setHidden:YES];
    //creation of tool bar
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
     tb = [[UIToolbar alloc] init];
    //[tb setBackgroundImage:[UIImage imageNamed:@"tabbar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    tb.tintColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
    {
        tb.frame = CGRectMake(0, delegate.window.frame.size.height-50, 320, 44);

    }
    else
    {
        tb.frame = CGRectMake(0, delegate.window.frame.size.height-70, 768, 44);
    }

    [delegate.window addSubview:tb];

但问题是在 iPad 中我想改变工具栏的方向但它不会改变它总是需要纵向宽度和高度。

4

2 回答 2

0

使用通知来检测设备方向,如下所示。

// Start Orientation //
- (void)orientationChanged:(NSNotification *)notification{
    [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {

    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        //NSLog(@"Vertical");

    }
    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        //NSLog(@"Horizontal");

    }
}
// End Orientation //

-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:nil];
}

有人在某处好心地写了这个通知代码。我没写。

于 2013-05-18T11:35:51.007 回答
0

使用 viewWillLayoutSubViews 方法并在该方法中将代码添加到工具栏的 setFrame

前任:

- (void) viewWillLayoutSubviews
{
     [toolBar setFrmae:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.height - 44, self.view.bounds.size.width, 44)];
}
于 2013-05-18T16:58:28.560 回答