簡體   English   中英

工具欄未在iOS中顯示

[英]Toolbar doesn't show up in iOS

我是iOS的新手,並使用以下代碼顯示工具欄。 相同的代碼適用於其他屏幕。

我已經調試了代碼,它進入了show工具欄方法,並且沒有任何異常地完成了viewDidLoad方法。 然后工具欄也不會出現。

但是它沒有顯示在UI的工具欄中。請幫助我解決此問題...

下面是實現代碼。

 @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

嘗試這個

[self.navigationController setToolbarHidden:NO animated:NO];

使用CGRect rootViewBounds = [UIScreen mainScreen] .bounds; 解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM