我有一個矢量影像,從
但我不希望此按鈕具有這些尺寸,而是那些設定為:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
現在,如果我去一個影像編輯器,并將這個矢量檔案的大小更改為22x22(它仍然是svg檔案),它會顯示如下:

這看起來是我想要的。
The thing is, I thought that Xcode will generate required png file from this svg based on device's pixel density. So in this case, say, on newer devices that use @3x images, at compile time, cause my button is defined as 32x32 points, a 96px X 96px image will be created and used as @3x image.
Also, I thought that this is going to work like described, no matter what "dimensions" a physical .svg file is, cause it's a vector, and it should be up/down scaled as view dictates. Where I am wrong with this, and how to make this button to look the same as from second image, no matter of what are actual .svg dimensions?
EDIT:
在 Attributes Inspector 中,對于這個資產,我設定了Preserve Vector Data并選擇了Single Scale選項。
uj5u.com熱心網友回復:
不使用自動布局約束的另一種方法是使用 for ,就像您評論的UIView()那樣customview
let view = UIView(frame: button.frame)
view.addSubview(button)
let item = UIBarButtonItem(customView: view)
self.navigationItem.setRightBarButtonItems([item], animated: true)
uj5u.com熱心網友回復:
當添加一個按鈕作為customView條形按鈕項時,UIKit 將自動使用自動布局。
因此,您的 init ofUIButton(frame: CGRect(x: 0, y: 0, width: 32, height: 32))沒有任何效果。
我從您鏈接到的站點中抓取了“alien.svg”,并在未編輯的情況下使用了它,并具有以下設定:

然后,使用此代碼(在 中viewDidLoad()):
let button = UIButton()
button.backgroundColor = .yellow
button.setImage(UIImage(named: "alien"), for: .normal)
button.setTitle(nil, for: .normal)
// adding the button as a custom bar button item view
// automatically uses auto-layout
button.widthAnchor.constraint(equalToConstant: 32).isActive = true
button.heightAnchor.constraint(equalTo: button.widthAnchor).isActive = true
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let item = UIBarButtonItem(customView: button)
// you can use either
//navigationItem.rightBarButtonItem = item
// or
navigationItem.setRightBarButtonItems([item], animated: true)
我們得到這個結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/451862.html
標籤:IOS 迅速 svg 按钮 uibarbuttonitem
下一篇:嵌入HTML重疊的SVG
