8

I want to put Custom image on my UINavigationBar,

When i tried this two code snippets,

[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]]];

And

    UIButton *logoView = [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,30)];
    [logoView setBackgroundImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];
    [logoView setUserInteractionEnabled:NO];
    self.navigationItem.titleView = logoView;

image which i can see on my Device is getting blur or it is getting streched.

I made image with Resolution of 180*40(Width*Height)

I want to know the exact size of Image. what, it would be ?

Thanks in advance.

4

3 回答 3

22

用这个

UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 98, 34)];
navigationImage.image=[UIImage imageNamed:@"topNav-logo.png"];

UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 98, 34)];
    [workaroundImageView addSubview:navigationImage];
    self.navigationItem.titleView=workaroundImageView;

希望这可以帮助。

于 2013-07-18T10:02:08.900 回答
3

创建尺寸(320 x 44)的图像并设置为如下所示的标题。

self.navigationItem.titleView = yourImageView;

请参阅下面的整个示例...

- (void)viewDidLoad{
        UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
        self.navigationItem.titleView = imgView;
        [imgView release];
}
于 2013-07-18T09:55:31.023 回答
1

你需要先初始化titleView。然后将它的 imageView 设置为子视图:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];

self.navigationItem.titleView = [[UIView alloc] initWithFrame:imageView.bounds];
[self.navigationItem.titleView addSubview:imageView];
于 2013-07-18T09:50:56.543 回答