OStack程序员社区-中国程序员成长平台

标题: ios - 自定义 UISearchBar [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:54
标题: ios - 自定义 UISearchBar

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

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

enter image description here



Best Answer-推荐答案


我曾经做过类似的事情,如果你使用 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

}

关于ios - 自定义 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615550/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4