自定义UISearchBar

时间:2016-02-24 23:48:22

标签: ios uisearchbar

我很好奇人们如何制作自己的搜索界面,我真的不喜欢标准而且决定自己编写。我不知道如何写自己的,所以我要求提示,在那里你可以看到我所知道的一切 - 这就是创建你自己UIView所需要的。

我想做的一个例子enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

我曾经做过类似的事情,如果你使用swift,研究这段代码,它会对你有所帮助,如果这对你的帮助有所帮助,请进一步解释。

enter image description here

func addNavigationBar()
{
    self.navigationController!.navigationBar.barTintColor = UIColor.blackColor()

    let centerView = UIView(frame:  CGRectMake(0, 0, 100, 30))

    let logo = UIImage(named: "logo.png")

    var btnMiddle = UIButton(frame: CGRectMake(0,5,80,20))
    btnMiddle.setBackgroundImage(logo, forState: UIControlState.Normal)
    btnMiddle.addTarget(self, action: "goBackHome", forControlEvents: UIControlEvents.TouchUpInside)
    centerView.addSubview(btnMiddle)

    self.navigationItem.titleView = centerView

        var buttonDimension = CGFloat(25)

        let rightView = UIView(frame:  CGRectMake(0, 0, 100, 30))
        let leftView = UIView(frame:  CGRectMake(0, 0, 100, 30))

        let btnr1 = UIButton(frame: CGRectMake(30,0,buttonDimension,buttonDimension))
        btnr1.setBackgroundImage(UIImage(named: "_barSearch.png"), forState: UIControlState.Normal)
        btnr1.addTarget(self, action: "search", forControlEvents: UIControlEvents.TouchUpInside)
        rightView.addSubview(btnr1)

        let btnr2 = UIButton(frame: CGRectMake(80,0,buttonDimension,buttonDimension))
        btnr2.setBackgroundImage(UIImage(named: "_barSettinges.png"), forState: UIControlState.Normal)
        btnr2.addTarget(self, action: "settinges", forControlEvents: UIControlEvents.TouchUpInside)
        rightView.addSubview(btnr2)

        let btnl3 = UIButton(frame: CGRectMake(0,0,buttonDimension,buttonDimension))
        btnl3.setBackgroundImage(UIImage(named: "_barMenu.png"), forState: UIControlState.Normal)
        btnl3.addTarget(self, action: "menu", forControlEvents: UIControlEvents.TouchUpInside)
        leftView.addSubview(btnl3)

        let rightBtn = UIBarButtonItem(customView: rightView)
        self.navigationItem.rightBarButtonItem = rightBtn

        let leftBtn = UIBarButtonItem(customView: leftView)
        self.navigationItem.leftBarButtonItem = leftBtn

}