在弹出窗口中更改颜色导航控制器 [英] change color navigation controller in a popover

查看:81
本文介绍了在弹出窗口中更改颜色导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试更改popoverController中导航控制器的颜色时出现问题。

我正在尝试这样做:

I'm having a problem with trying to change the colour of my navigation controller inside my popoverController.
I'm trying to do something like this:

if (self.popoverController == nil) {
    ArtistsViewController *artistsViewController = 
    [[ArtistsViewController alloc]      
     initWithNibName:@"ArtistsViewController" 
     bundle:[NSBundle mainBundle]]; 

    artistsViewController.navigationItem.title = @"Artists";
    UINavigationController *navController = 
    [[UINavigationController alloc] 
     initWithRootViewController:artistsViewController];


    UIPopoverController *popover = 
    [[UIPopoverController alloc] 
     initWithContentViewController:artistsViewController.navigationController];
    artistsViewController.navigationController.navigationBar.tintColor=[UIColor greenColor];

    popover.delegate = self;
    [artistsViewController release];
    [navController release];

    self.popoverController = popover;
    [popover release];
}

[self.popoverController 
 presentPopoverFromBarButtonItem:sender                                    
 permittedArrowDirections:UIPopoverArrowDirectionAny 
 animated:YES];

但它不起作用。有什么建议吗?

but it doesn't work. any suggestions?

推荐答案

////////////////////// //////////////////

////////////////////////////////////////

//////////// ** 解决方案 ** ////////

////////////**SOLUTION**////////

///////////// ///////////////////////////

////////////////////////////////////////

我要编辑这篇文章,因为我解决了我的问题,我认为可以在这里分享我的解决方案:

I'm gonna edit this post because i solved my problem and I think could be helpful share my solution here:

首先,请按照以下示例:

http://mobiforge.com/designing/story/ using-popoverview-ipad-app-development

完成后转到第二步。

第二步将添加一个新的popoverBackgroundViewClass:

The second step will be add a new popoverBackgroundViewClass:

在项目目标类中添加新文件并将其命名为'CustomPopoverBackgroundView'

Add new file in your project Objective class and call it 'CustomPopoverBackgroundView'

///////////////////////////////////////////////
////// CustomPopoverBackgroundView.h  /////////
///////////////////////////////////////////////

     #import < UIKit/UIKit.h >
     #import < UIKit/UIPopoverBackgroundView.h >

    @interface CustomPopoverBackgroundView : UIPopoverBackgroundView{
        UIPopoverArrowDirection direction;
        CGFloat offset;
    }

    @end

//////////////////////////////////////////////
////// CustomPopoverBackgroundView.m /////////
//////////////////////////////////////////////


    #import "CustomPopoverBackgroundView.h"

    @implementation CustomPopoverBackgroundView

     -  (void)layoutSubviews {
        [super layoutSubviews];
        CGFloat fullHeight = self.frame.size.height;
        CGFloat fullWidth = self.frame.size.width;
        CGFloat startingLeft = 0.0;
        CGFloat startingTop = 0.0;
        CGFloat arrowCoord = 0.0;

        UIImage *arrow;
        UIImageView *arrowView;

        switch (self.arrowDirection) {
            case UIPopoverArrowDirectionUp:
                startingTop += 13.0;
                fullHeight -= 13.0;
    //the image line.png will be the corner 
                arrow = [UIImage imageNamed:@"line.png"];
                arrowCoord = (self.frame.size.width / 2) - self.arrowOffset;
                arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(arrowCoord, 0, 13.0, 13.0)] autorelease];
                break;


            case UIPopoverArrowDirectionDown:
                fullHeight -= 13.0;
                arrow = [UIImage imageNamed:@"line.png"];
                arrowCoord = (self.frame.size.width / 2) - self.arrowOffset;
                arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(arrowCoord, fullHeight, 13.0, 13.0)] autorelease];
                break;

            case UIPopoverArrowDirectionLeft:
                startingLeft += 13.0;
                fullWidth -= 13.0;
                arrow = [UIImage imageNamed:@"line.png"];
                arrowCoord = (self.frame.size.height / 2) - self.arrowOffset;
                arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, arrowCoord, 13.0, 13.0)] autorelease];
                break;

            case UIPopoverArrowDirectionRight:
                fullWidth -= 13.0;
                arrow = [UIImage imageNamed:@"line.png"];
                arrowCoord = (self.frame.size.height / 2) - self.arrowOffset;
                arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-13.0, arrowCoord, 13.0, 13.0)] autorelease];
                break;

        }


        //this image will be your background
        UIImage *bg = [[UIImage imageNamed:@"lineBack.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0)];
        UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(startingLeft, startingTop, fullWidth, fullHeight)] autorelease];
        [imageView setImage:bg];
        [arrowView setImage:arrow];
        [self addSubview:imageView];
        [self addSubview:arrowView];
    }



    - (CGFloat) arrowOffset {

        return offset;

    }



    - (void) setArrowOffset:(CGFloat)arrowOffset {

        offset = arrowOffset;

        [self setNeedsLayout];

    }



    - (UIPopoverArrowDirection)arrowDirection {

        return direction;

    }



    - (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection {

        direction = arrowDirection;

        [self setNeedsLayout];

    }



    + (CGFloat)arrowHeight {

        return 30.0;

    }



    + (CGFloat)arrowBase {

        return 30.0;

    }



    + (UIEdgeInsets)contentViewInsets {

        return UIEdgeInsetsMake(30.0, 30.0, 30.0, 30.0);

    }

    @end

第三步:

完成后,在'PopOverExample1ViewController.m'中添加以下行:

When It is done add this line in 'PopOverExample1ViewController.m':

导入新类:

#import " CustomPopoverBackgroundView.h "


-(IBAction) showMovies:(id) sender {

    if (self.popoverController == nil) {
        MoviesViewController *movies = 
            [[MoviesViewController alloc] 
                initWithNibName:@"MoviesViewController" 
                         bundle:[NSBundle mainBundle]]; 

        UIPopoverController *popover = 
            [[UIPopoverController alloc] initWithContentViewController:movies]; 

        popover.delegate = self;
        [movies release];


//THIS IS THE LINE THAT YOU HAVE TO ADD


         popover.popoverBackgroundViewClass=[CustomPopoverBackgroundView class];


        self.popoverController = popover;
        [popover release];
    }

    [self.popoverController 
        presentPopoverFromBarButtonItem:sender 
               permittedArrowDirections:UIPopoverArrowDirectionAny 
                               animated:YES];    
}

-(IBAction) btnShowMovies:(id) sender {

    if (self.popoverController == nil) {
        MoviesViewController *movies = 
            [[MoviesViewController alloc] 
                initWithNibName:@"MoviesViewController" 
                         bundle:[NSBundle mainBundle]]; 

        UIPopoverController *popover = 
            [[UIPopoverController alloc] initWithContentViewController:movies]; 

        popover.delegate = self;
        [movies release];


//THIS IS THE LINE THAT YOU HAVE TO ADD


         popover.popoverBackgroundViewClass=[CustomPopoverBackgroundView class];


        self.popoverController = popover;
        [popover release];
    }

    CGRect popoverRect = [self.view convertRect:[btn frame] 
                                       fromView:[btn superview]];

    popoverRect.size.width = MIN(popoverRect.size.width, 100); 
    [self.popoverController 
        presentPopoverFromRect:popoverRect 
                        inView:self.view 
      permittedArrowDirections:UIPopoverArrowDirectionAny 
                      animated:YES];
}

好吧!就这样!如果someOne需要帮助,请告诉我。
祝福!

Alright!!! Thats all! If someOne needs help just let me know. Best wishes!

这篇关于在弹出窗口中更改颜色导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆