如何更改添加到窗口的工具栏的方向

时间:2013-05-18 10:59:45

标签: iphone ios objective-c ipad

我正在开发一个基于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上我想改变工具栏的方向,但它不会改变它总是需要纵向宽度和高度。

2 个答案:

答案 0 :(得分: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];
}

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

答案 1 :(得分: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)];
}