ios - 如何以编程方式插入右键图像UINavigationbar swift 3
<p><p>我正在尝试使用静态图像以编程方式覆盖导航项的 rightButton </p>
<p><strong>这样的按钮:</strong></p>
<p> <a href="/image/s4L4D.png" rel="noreferrer noopener nofollow"><img src="/image/s4L4D.png" alt=""/></a> </p>
<p>我已经尝试了很多时间来解决它,但没有得到正确的答案。</p>
<p>谁能帮我做到这一点?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>可能的解决方案。您可以创建 UIBarButton 项目的扩展。
示例:</p>
<pre><code>import UIKit
extension UIBarButtonItem {
convenience init (image: UIImage?, target: Any?, action: Selector?) {
let button = UIButton(type: .custom)
button.imageView?.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0.0, y: 0.0, width: 44.0, height: 44.0)
button.addTarget(target, action: action!, for: .touchUpInside)
button.setImage(image, for: UIControlState.normal)
button.imageEdgeInsets = UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0)
if #available(iOS 9.0, *) {
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
}
self.init(customView: button)
}
}
</code></pre>
<p>然后在你的 <code>viewDidLoad</code> 方法中使用它:</p>
<pre><code>let rightButton = UIBarButtonItem(image: UIImage(named: "example"), target: self, action: #selector(exampleAction(sender:)))
self.navigationItem.rightBarButtonItem = rightButton
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何以编程方式插入右键图像UINavigationbar swift 3,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48316280/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48316280/
</a>
</p>
页:
[1]