2015-05-05 127 views
1

我想将图像添加到导航栏的右侧。但我无法将图像视图拖动到导航栏。我只能拖动一个按钮。将imageview放在导航栏上

enter image description here

我怎样才能做到这一点?我想与whatsapp配置文件图像相同的东西。你知道他们在导航栏的右侧有个人资料图片。

回答

7

首先拖动UIView并将其放置在导航栏上。然后将UIImageView拖入视图中。

这应该给你你在找什么。

使用约束设置图像视图的高度和宽度。您必须在尺寸检查器中设置包含视图的宽度和高度。

1

不知道故事板。但是从代码中,您可以创建具有自定义视图的UIBarButtonItem

 let customView = UIImageView() 
     let customViewItem = UIBarButtonItem(customView: customView) 
     self.navigationItem.rightBarButtonItem = customViewItem 

从故事板:

只需拖动UIBarButtonItem到控制器中的导航栏。在元素菜单(属性检查器)中选择标识符:Custom和Image:你想要的图像。

+0

这个代码是如何知道在何处放置?我的意思是为什么这段代码将图像放在右边而不是左边? – Okan

+0

这只是一个如何创建它的例子。 –

1
**This will Display the image on right side on navigation bar** 

enter image description here

func viewDidLoad(){ 
     let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) 
       let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) 
       imageview.image = UIImage(named: "photo.jpg") 
       imageview.contentMode = UIViewContentMode.scaleAspectFit 
       imageview.layer.cornerRadius = 20 
       imageview.layer.masksToBounds = true 
       containView.addSubview(imageview) 
       let rightBarButton = UIBarButtonItem(customView: containView) 
       self.navigationItem.rightBarButtonItem = rightBarButton 
     }