2015-07-11 74 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