【问题标题】:Recreating Apple Music UINavigationController title arrow重新创建 Apple Music UINavigationController 标题箭头
【发布时间】:2015-07-02 19:37:55
【问题描述】:

我正在尝试从 Apple 新音乐应用中的“新建”标签创建外观:

起初,我以为箭头只是一个 unicode 字符,但我发现最接近的符号是:﹀,而且这不是垂直对齐的,所以这段代码产生了以下内容

navigationItem.title = "All Genres﹀"
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.redColor()]

你有什么建议我会怎么做?

【问题讨论】:

    标签: swift uinavigationcontroller


    【解决方案1】:

    我使用@rounak 的解决方案让它工作了。这是任何想知道的人的代码

    let arrow = UIImage(named: "titleArrow")!.imageWithRenderingMode(.AlwaysOriginal)
    
    let titleButton = UIButton(type: .System)
    titleButton.addTarget(self, action: "pickGenre", forControlEvents: .TouchUpInside)
    
    titleButton.setTitle("All Genres", forState: .Normal)
    titleButton.setTitleColor(UIColor.redColor(), forState: .Normal)
    titleButton.titleLabel!.font = UIFont.systemFontOfSize(16)
    
    titleButton.setImage(arrow, forState: .Normal)
    titleButton.sizeToFit()
    
    titleButton.titleEdgeInsets = UIEdgeInsetsMake(0, -titleButton.imageView!.frame.width, 0, titleButton.imageView!.frame.width)
    titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, titleButton.titleLabel!.frame.width + 2.5, 0, -titleButton.titleLabel!.frame.width)
    
    navigationItem.titleView = titleButton
    

    titleArrow@2x.png:

    【讨论】:

      【解决方案2】:

      我会通过使用 UIButton 来做到这一点。将其图像设置为箭头图像,并将其标题设置为 All Genres。无论如何,当它被点击时你会想要一个回调,所以一个按钮在功能上也能很好地发挥作用。

      将按钮分配给 navigationItem 的 titleView。

      【讨论】:

        【解决方案3】:

        上述 Swift 3 版本:

            let arrow = UIImage(named: "titleArrow")!.withRenderingMode(.alwaysOriginal)
        
            let titleButton = UIButton(type: .system)
            titleButton.addTarget(self, action: #selector(pickGenre), for: .touchUpInside)
        
            titleButton.setTitle("All Genres", for: .normal)
            titleButton.setTitleColor(.red, for: .normal)
            titleButton.titleLabel!.font = UIFont.systemFont(ofSize: 16)
        
            titleButton.setImage(arrow, for: .normal)
            titleButton.sizeToFit()
        
            titleButton.titleEdgeInsets = UIEdgeInsetsMake(0, -titleButton.imageView!.frame.width, 0, titleButton.imageView!.frame.width)
            titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, titleButton.titleLabel!.frame.width + 2.5, 0, -titleButton.titleLabel!.frame.width)
        
            navigationItem.titleView = titleButton
        

        【讨论】:

          猜你喜欢
          • 2018-10-27
          • 2018-06-02
          • 1970-01-01
          • 2011-03-19
          • 2017-11-14
          • 2020-06-29
          • 1970-01-01
          • 2022-08-07
          • 1970-01-01
          相关资源
          最近更新 更多