简体   繁体   English

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

[英]How to change orientation of toolbar which is adding to window

I am developing one app which is based on tabbarcontroller.In one view Controller i want to hidden the tabbarController instead of that i want to show toolbar for that purpose i wrote the following code 我正在开发一个基于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];

But problem is in iPad i want to change orientation of toolbar but it does not change it always takes portrait width and height. 但是问题是在iPad中,我想更改工具栏的方向,但它不会改变,它始终需要纵向和纵向。

Use notification to detect the device orientation like the following. 使用通知来检测设备方向,如下所示。

// 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];
}

Somebody has kindly written this notification code somewhere. 有人在某处编写了此通知代码。 I didn't write it. 我没写

use viewWillLayoutSubViews method and add code to setFrame of toolbar in that method 使用viewWillLayoutSubViews方法并将代码添加到该方法的工具栏的setFrame中

ex: 例如:

- (void) viewWillLayoutSubviews
{
     [toolBar setFrmae:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.height - 44, self.view.bounds.size.width, 44)];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM