-->

iPhone: Add button on MPMoviePlayerViewController&

2019-08-02 18:00发布

问题:

I am trying to add button on MPMoviePlayerViewController's navigationcontroller.view. I have wrote following so far. It doesn't give any error but button isn't appearing on view! Could anyone please tell me what am I doing wrong? Thanks.

        MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];
        UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
        favButton.frame = CGRectMake(280, 25, 30, 30);
        [favButton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [favButton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
        [videoController.navigationController.view addSubview:favButton];
        [self presentMoviePlayerViewControllerAnimated:videoController];


- (void)favouriteButtonClicked:(id)sender
{
    NSLog(@"Inside favourite button clicked");
}

Also try adding using following code but no luck!

        UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)]; 
        videoController.navigationController.navigationItem.rightBarButtonItem=button; 
        [button release];

回答1:

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)]; 
videoController.navigationItem.rightBarButtonItem = button; 
[button release];


回答2:

If you use presentModalViewController, you can not see the button you add. You have to [self.navigationController pushViewController:moviePlayer animated:YES] to see it.



回答3:

Try this..it will work

[[[UIApplication sharedApplication] keyWindow] addSubview:favButton];