图片为导航栏按钮项目swift

时间:2023-01-16 12:19:57

I want to display an image in the left hand side of my nav bar in swift.

我想在我的导航栏的左手边显示一个图片。

I have tried adding a nav bar button item and setting an image there.

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

The problem is that I have to use a really small image for it to fit in the nav bar nicely. But making such a small image leads to pixelation especially on the bigger phone iPhone 6 and 6 Plus.

问题是我必须用一个很小的图像才能很好地融入导航条。但制作如此小的图像会导致像素化,尤其是在更大的iPhone 6和6 Plus上。

Is there a way to use a good quality image and then set the frame to fit within the bounds of the nav bar?

是否有一种方法可以使用高质量的图像,然后将帧设置在导航条的范围内?

My attempt:

我的尝试:

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)

I tried putting the frame on the image first and then on the bar button item. But this is throwing up an error:

我试着先把框架放在图像上,然后放在栏按钮上。但这是一个错误:

Type of expression is ambiguous without more context.

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

3 个解决方案

#1


41  

Try This

试试这个

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]

For Swift 3

斯威夫特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

Here is action

这是行动

func callMethod() {
//do stuff here
}

#2


3  

Use this code:

使用这段代码:

self.navigationItem.leftBarButtonItem = nil

let button = UIButton(type: .custom)
button.setImage(UIImage (named: "ChatTab"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)

let button2 = UIButton(type: .custom)
button2.setImage(UIImage (named: "ActivityTab"), for: .normal)
button2.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)

let barButtonItem2 = UIBarButtonItem(customView: button2)
self.navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]

Output: 图片为导航栏按钮项目swift

输出:

#3


1  

There is a way to use images of different sizes, depending on the device. It's called an Asset Catalog. You'll probably already have one in your project, or if not, you can add one with File > New > File > Resource > Asset Catalogue.

根据设备的不同,可以使用不同大小的图像。它叫做资产目录。您的项目中可能已经有了一个,或者如果没有,您可以添加一个带有文件>的新>文件>资源>资产目录。

Within your Asset Catalog, you can have multiple 'Image Sets' (these will be shown down the left-hand side). Add a new Image Set with the '+' at the bottom. For each Image Set, you can supply different images (e.g. of different sizes) for each of @1x, @2x, and @3x.

在您的资产目录中,您可以有多个“映像集”(这些将显示在左边)。在底部添加一个带有“+”的新图像集。对于每个图像集,您可以为每一个@1x、@2x和@3x提供不同的图像(例如不同大小的图像)。

Then, to use one of these images in code, you simply use UIImage(named: "name_of_image_set") - note no extension. The correct image will be loaded, depending on the device.

然后,要在代码中使用其中一个图像,只需使用UIImage(命名为:“name_of_image_set”)——注意没有扩展。根据设备的不同,将加载正确的图像。

Hope this helps!

希望这可以帮助!

#1


41  

Try This

试试这个

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]

For Swift 3

斯威夫特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

Here is action

这是行动

func callMethod() {
//do stuff here
}

#2


3  

Use this code:

使用这段代码:

self.navigationItem.leftBarButtonItem = nil

let button = UIButton(type: .custom)
button.setImage(UIImage (named: "ChatTab"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)

let button2 = UIButton(type: .custom)
button2.setImage(UIImage (named: "ActivityTab"), for: .normal)
button2.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)

let barButtonItem2 = UIBarButtonItem(customView: button2)
self.navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]

Output: 图片为导航栏按钮项目swift

输出:

#3


1  

There is a way to use images of different sizes, depending on the device. It's called an Asset Catalog. You'll probably already have one in your project, or if not, you can add one with File > New > File > Resource > Asset Catalogue.

根据设备的不同,可以使用不同大小的图像。它叫做资产目录。您的项目中可能已经有了一个,或者如果没有,您可以添加一个带有文件>的新>文件>资源>资产目录。

Within your Asset Catalog, you can have multiple 'Image Sets' (these will be shown down the left-hand side). Add a new Image Set with the '+' at the bottom. For each Image Set, you can supply different images (e.g. of different sizes) for each of @1x, @2x, and @3x.

在您的资产目录中,您可以有多个“映像集”(这些将显示在左边)。在底部添加一个带有“+”的新图像集。对于每个图像集,您可以为每一个@1x、@2x和@3x提供不同的图像(例如不同大小的图像)。

Then, to use one of these images in code, you simply use UIImage(named: "name_of_image_set") - note no extension. The correct image will be loaded, depending on the device.

然后,要在代码中使用其中一个图像,只需使用UIImage(命名为:“name_of_image_set”)——注意没有扩展。根据设备的不同,将加载正确的图像。

Hope this helps!

希望这可以帮助!