我想将自定义图像放在我的UINavigationBar上,
当我尝试这两段代码片段时,
[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]]];
和
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;
我可以在设备上看到的图像变得模糊或者拉伸了。
我制作的图像的分辨率为180*40
(宽*高)
我想知道Image的确切大小。什么,会是什么?
提前致谢。
答案 0 :(得分:21)
使用此
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;
希望这会有所帮助。
答案 1 :(得分: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];
}
答案 2 :(得分:1)
您需要先初始化titleView。然后将imageView设置为subView:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
self.navigationItem.titleView = [[UIView alloc] initWithFrame:imageView.bounds];
[self.navigationItem.titleView addSubview:imageView];