菜鸟教程小白 发表于 2022-12-11 19:19:00

ios - 如何将按钮设置到导航的最右侧 - iOS


                                            <p><p>我在导航栏右侧设置了一个按钮:</p>

<p>我的 ViewController :
<a href="/image/52Y30.png" rel="noreferrer noopener nofollow"><img src="/image/52Y30.png" alt="enter image description here"/></a> </p>

<p>我想为这个按钮设置一个汉堡图标、红色循环和一个标签。像这样:</p>

<p> <a href="/image/dSJ8s.png" rel="noreferrer noopener nofollow"><img src="/image/dSJ8s.png" alt="enter image description here"/></a> </p>

<p>我的代码:</p>

<pre><code>    self.navigationController?.navigationBar.barTintColor = self.utilities.hexStringToUIColor(hex: &#34;#00b8de&#34;)

    var imageview2 = UIImage(named: &#34;menulogo&#34;)

    imageview2 = imageview2?.imageResize(sizeChange: CGSize(width: 25, height: 25))

    btnMenu.setImage(imageview2,for:UIControlState.normal)
    btnMenu.setTitle(&#34;&#34;, for: .normal)


    // setup the red circle UIView
    let redCircleView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    redCircleView.backgroundColor = UIColor.red
    redCircleView.layer.cornerRadius = view.frame.size.width / 2

    // setup the number UILabel
    let label = UILabel(frame: CGRect(x: 30, y: 0, width: 20, height: 20))
    label.textColor = UIColor.white
    label.font = UIFont.systemFont(ofSize: 10)
    label.text = &#34;16&#34;

    // adding the label into the red circle
    redCircleView.addSubview(label)

    // adding the red circle into the menu button
    btnMenu.addSubview(redCircleView)
</code></pre>

<p>使用上面的代码我有三个问题:</p>

<ol>
<li>我的汉堡图片不在导航的最右侧。</li>
<li>我的自行车 View 没有显示</li>
<li>我的图标是白色的,但它显示为蓝色!</li>
</ol>

<p><code>btnMenu</code> 是我的导航按钮。</p>

<p> <a href="/image/OT06j.png" rel="noreferrer noopener nofollow"><img src="/image/OT06j.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我已经使用这个库在导航按钮上设置角标(Badge)。</p>

<blockquote>
<p>MIBadgeButton is badge button written in Swift with high
UITableView/UICollectionView performance.</p>
</blockquote>

<p> <a href="https://github.com/mustafaibrahim989/MIBadgeButton-Swift" rel="noreferrer noopener nofollow">https://github.com/mustafaibrahim989/MIBadgeButton-Swift</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何将按钮设置到导航的最右侧 - iOS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42898344/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42898344/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何将按钮设置到导航的最右侧 - iOS