当前位置: 首页 > 面试题库 >

导航栏按钮项目swift的图像

羿昊英
2023-03-14
问题内容

我想在导航栏的左侧快速显示图像。

我尝试添加导航栏按钮项并在那里设置图像。

问题是我必须使用非常小的图像才能使其很好地适应导航栏。但是制作如此小的图像会导致像素化,特别是在较大的手机iPhone 6和6 Plus上。

有没有一种方法可以使用高质量的图像,然后将框架设置为适合导航栏的边界?

我的尝试:

var image = UIImage(named: "Harp.png")

image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationItem.leftBarButtonItem.frame = CGRectMake(0, 0, 53, 31)
//image.frame = CGRectMake(0, 0, 53, 31)

我尝试先将框架放在图像上,然后再放在条形按钮项上。但这抛出了一个错误:

没有更多上下文的情况下,表达类型不明确。


问题答案:

试试这个

let button = UIButton(type: UIButtonType.Custom)
button.setImage(UIImage(named: "yourImageName.png"), forState: UIControlState.Normal)
button.addTarget(self, action:Selector("callMethod"), forControlEvents: UIControlEvents.TouchDragInside)
button.frame=CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItems = [newBackButton,barButton]

对于Swift 3

let button = UIButton.init(type: .custom)
button.setImage(UIImage.init(named: "yourImageName.png"), for: UIControlState.normal)
button.addTarget(self, action:#selector(ViewController.callMethod), for:.touchUpInside)
button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30) //CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem.init(customView: button)
            self.navigationItem.leftBarButtonItem = barButton

斯威夫特4

let button = UIButton(type: UIButton.ButtonType.custom)
button.setImage(UIImage(named: "getstarted"), for: .normal)
button.addTarget(self, action:#selector(callMethod), for: .touchDragInside)
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItems = [barButton]

这是行动

@objc func callMethod() {   
    //do stuff here
 }


 类似资料:
  • 问题内容: 此代码在ios10中可以正常工作。我得到我的标签和一个图像按钮,该按钮是用户照片资料,圆形。.好的。但是当运行xcode 9 ios11模拟器时,我将其拉长了。当检查sim并获取视图并告诉xcode描述视图时,按钮帧必须为32x32,我将输出为170x32或类似的东西。 这是我的代码。 问题答案: 原因 出现问题是因为从ios 11 使用自动布局而不是处理帧。 解 如果使用Xcode

  • 在寻求了很多,尝试了很多解决方案后,没有解决我的问题。在我的应用程序中,我自定义了UINavigationController以获得模糊效果: 类CustomNavigationController:UINavigationController{ 然后,在Main.Storyboard中,我为导航控制器项选择了自定义类。 模糊效果工作正常,状态图标正确可见,但不是标准的导航栏项:左按钮,标题和右按

  • 使用LeftBarItems和RightBarItems时,我遇到过过多的UIBarButtonItem填充/间距(请参见下图)。UIBarButtonims上使用的图标不包含额外的填充。我想知道是什么原因造成的?

  • 我一直在尝试为我的应用程序获得一个透明的导航栏,但我已经尝试到目前为止,并没有给我的结果,我正在寻找。 作为一个参考示例,下面是“Play Store”导航栏的外观: 注意,它是一个黑暗但透明的颜色。 我在应用程序中尝试了以下内容: 我已经将我的主题和单个支架的背景颜色设置为深色,但我的导航栏仍然是白色的,如下图所示: 正如你在上图中看到的,状态栏接受它的透明颜色,并尊重应用背景,但导航栏不接受。

  • ap.hideOptionButton() 隐藏导航栏右侧按钮。 代码示例 <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script> <button class="btn btn-default">显示多个按钮</button> <button

  • ap.showOptionButton() 显示导航栏右侧按钮。 代码示例 <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script> <button class="btn btn-default">显示 optionButton</button