2015-07-11 73 views
0

我想将导航栏的titleView设置为舍入图像。与通常在消息传递应用程序中看到的个人资料图像类似圆形导航栏图像iphone sdk

我相信我应该能够通过执行以下操作来创建一个圆形按比例缩小图像:

UIImageView* profileImageView = [[UIImageView alloc] initWithImage:logoImage]; 
[profileImageView setFrame:CGRectMake(0, 0, 30, 30)]; 

//profileImageView.contentMode = UIViewContentModeScaleAspectFit; 
// XXX contentMode commented out because enabling it causes the rounded corners to have no effect? 
profileImageView.layer.cornerRadius = 15; 
profileImageView.layer.masksToBounds = YES; 
profileImageView.clipsToBounds = YES; 
self.navigationItem.titleView = profileImageView; 

这似乎创造我的一瞬间想要第二个当我加载仿真器中的图像,但图像出现在屏幕的左上角,然后立即捕捉到导航栏的中心。一旦位于导航栏的中心,它就会重新调整以占据导航栏的整个空间,而不是保留为小圆圈。我错过了什么?似乎我必须禁用什么机制导致我的图像扩大到填满整个导航栏。

回答

5

只需将它添加到包含视图

UIView * containView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; 
UIImageView* profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpeg"]]; 
[profileImageView setFrame:CGRectMake(0, 0, 30, 30)]; 
profileImageView.layer.cornerRadius = 15; 
profileImageView.layer.masksToBounds = YES; 
profileImageView.clipsToBounds = YES; 
[containView addSubview:profileImageView]; 
self.navigationItem.titleView = containView; 

截图

enter image description here

+0

嘿,这伟大的工程,但我怎么可以添加一个白色的边框,以四舍五入的图像? – PhoenixDev

+0

_profileImageView.layer.borderWidth = 1.0F; _ \t _profileImageView.layer.borderColor = [UIColor whiteColor] .CGColor; _ – Apoc