工具栏未在iOS中显示

安贾利

我是iOS的新手,并使用以下代码显示工具栏。相同的代码适用于其他屏幕。

我已经调试了代码,它进入了show工具栏方法,并且没有任何异常地完成了viewDidLoad方法。然后工具栏也不会出现。

但是它没有显示在用户界面中的工具栏上。请帮助我解决此问题...

下面是实现代码。

 @implementation HistoryViewController

    @synthesize wasLandscape;

    - (HistoryViewController *)init {
    if (self = [super init]) {
        //Set App Delegate
        appDelegate = [[UIApplication sharedApplication] delegate];

        //Set up an encyclopedia object for me
        encBook = [[Encyclopedia alloc] init];
        [[appDelegate prefsdb] loadHistoryEncyclopedia:encBook];
    }

    return self;
}

    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}

    //Handle appearance
    - (void) didRotateFromInterfaceOrientation:            (UIInterfaceOrientation)interfaceOrientation
{
//Kill the old, make the new
    [self hideToolbar];
    [self showToolbar];
}

   //Handle appear include Toolbar
   - (void)viewWillAppear:(BOOL)animated {
    [self showToolbar];
    [super viewWillAppear:animated];
}

//Remove the toolbar if we are disappearing
- (void)viewWillDisappear:(BOOL)animated {
    [self hideToolbar];
    [super viewWillDisappear:animated];
}


//Destroy the toolbar
- (void) hideToolbar {
    if(toolbar != nil){
        //Remove what we added
        [toolbar removeFromSuperview];
        [toolbar release];
        toolbar = nil;
    }
}


//Make the toolbar
- (void) showToolbar {
    BOOL landscape = (self.interfaceOrientation == UIDeviceOrientationLandscapeLeft || self.interfaceOrientation == UIDeviceOrientationLandscapeRight);

    // double check
    if (!landscape) {
        if (wasLandscape) {
            landscape = YES;
            wasLandscape = NO;
        }
    }

    //iOS4 accomodation
    if(toolbar != nil){
        [toolbar removeFromSuperview];
        [toolbar release];
        toolbar = nil;
    }

    //Initialize the toolbar -----------------------------------------------------------
    toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;

    //Set the toolbar to fit the width of the app.
    [toolbar sizeToFit];

    //Caclulate the height of the toolbar
    CGFloat toolbarHeight = [toolbar frame].size.height;

    //Get the bounds of the parent view
    CGRect rootViewBounds = self.parentViewController.view.bounds;

    //Get the height of the parent view.
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

    //Get the width of the parent view,
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

    //Create a rectangle for the toolbar
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

    //Reposition and resize the receiver
    [toolbar setFrame:rectArea];

    // create buttons

    UIBarButtonItem *bookmarkButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(showBookmarks:)];
    //bookmarkButton.TintColor = [UIColor colorWithRed:0.0 green:0.31 blue:0.56 alpha:1.0];
    // for spacing
    UIBarButtonItem *fixer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    UIBarButtonItem *fixer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    UIBarButtonItem *fixer3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    UIBarButtonItem *fixer4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

    if (!landscape) {
        fixer1.width = 32;
        fixer2.width = 32;
        fixer3.width = 32;
        fixer4.width = 32;
    } else {
        fixer1.width = 78;
        fixer2.width = 78;
        fixer3.width = 78;
        fixer4.width = 78;
    }

    [toolbar setItems:[NSArray arrayWithObjects:fixer4, bookmarkButton, nil]];


    //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];    

}
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // The method hideStatusbar called after 2 seconds
    [self showToolbar];
    // Do any additional setup after loading the view, typically from a nib.
}

@end
安贾利

使用CGRect rootViewBounds = [UIScreen mainScreen] .bounds; 解决了问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

CKEDITOR:我的工具栏中未显示某些图标

来自分类Dev

iOS-工具栏未显示在顶部

来自分类Dev

使用Xamarin Forms在iOS中显示保存工具栏按钮

来自分类Dev

在Android工具栏中显示图标

来自分类Dev

在Android工具栏中显示图标

来自分类Dev

android标题不会显示在工具栏中

来自分类Dev

标题等未显示在Android工具栏中

来自分类Dev

工具栏和操作栏未显示在活动中

来自分类Dev

?attr /未在工具栏中设置正确的颜色

来自分类Dev

Materialdesign工具栏的向后箭头未在Lollipop之前的版本中显示

来自分类Dev

在工具栏中显示菜单溢出按钮

来自分类Dev

无法获取工具栏以显示在Node Webkit中

来自分类Dev

Android-在工具栏中显示文件路径

来自分类Dev

我想始终在响应式(Safari)中显示工具栏ios

来自分类Dev

Searchview未显示在工具栏中

来自分类Dev

数据绑定中未显示工具栏

来自分类Dev

Tableau Public工具栏未在某些报告中显示

来自分类Dev

图片未显示在工具栏中

来自分类Dev

iOS工具栏未显示导航控制器

来自分类Dev

隐藏/显示工具栏

来自分类Dev

未在“工具栏”中指定muiTheme

来自分类Dev

工具栏显示在导航栏中

来自分类Dev

平铺未在折叠工具栏中居中

来自分类Dev

Activity 中的停止工具栏显示在 Fragments 中

来自分类Dev

工具栏未在其他活动中显示

来自分类Dev

工具栏未在 Android 的 API 级别 19 中显示

来自分类Dev

工具栏未显示在 Fragment 中

来自分类Dev

Laravel 5 中的 CKEditor 4 未在工具栏中显示 iFrame 图标

来自分类Dev

工具栏不显示