如何在导航栏中添加自定义视图?

时间:2016-11-02 04:39:05

标签: ios iphone swift uinavigationbar

我想在导航栏上创建自定义视图。我在导航栏上有三个项目。在左边我想在UILabel&之后添加UIImageView。右边是一个UIImage.Now我在故事板和右边添加了右边的按钮。添加了图片。我想添加Imageview& UILabel到左侧。请告诉我如何添加这个以便正确维持差距。

enter image description here

1 个答案:

答案 0 :(得分:0)

//FETECH BUTTON IMAGES FOR BOTH STATES NORMAL AND SELECTED
var normal= UIImage(contentsOfFile:NSBundle.mainBundle().pathForResource("FIRSTBUTTONIMG", ofType: ".png" , inDirectory: "FOLDER_PATH")!);

normal = normal?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

var selected = UIImage(contentsOfFile:NSBundle.mainBundle().pathForResource("FIRSTBUTTONIMGSELECTED ", ofType: ".png" , inDirectory: "FOLDER_PATH")!);

selected = selected?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

let Fbutton = UIButton(type: .Custom);

Fbutton.setImage(normal, forState: UIControlState.Normal)

Fbutton.setImage(selected, forState: UIControlState.Selected)

Fbutton.addTarget(self, action:#selector(YOURBTNACTION(_:)), forControlEvents: .TouchUpInside)

Fbutton.frame=CGRectMake(0, -6, 53, 53)

//ADD YOUR CUSTOM VIEW AND ADD ABOVE BUTTON INSIDE CUSTOM VIEW
let CustomView = UIView()

CustomView.frame=CGRectMake(0, 0, Fbutton.frame.height, Fbutton.frame.width);

CustomView.addSubview(Button)

//ADD MORE ITEMS  TO CUSTOM VIEW IF NEEDED

//ADD THAT CUSTOM VIEW TO UIBarButtonItem

let CustomViewButton:UIBarButtonItem = UIBarButtonItem(customView: backCustomView)

//ASSIGN THAT UIBarButtonItem TO leftBarButtonItems AS ARRAY ITEM
self.navigationItem.leftBarButtonItems=[CustomViewButton,MORE ITEM IF NEEDED];