iOS Swift将SVG图像添加到Android按钮

Android中,我们可以将SVG导入为Vector XML,
使用它作为Drawable,
更改SVG图标的颜色并添加到按钮

void setSvgIcnForBtnFnc(Button setBtnVar,int setSvgVar,int setClrVar,String PosVar)
{
    Drawable DevDmjVar = getDrawable(setSvgVar);
    DevDmjVar.setBounds(0,Dpx24,Dpx24);
    DevDmjVar.setColorFilter(new PorterDuffColorFilter(setClrVar,PorterDuff.Mode.SRC_IN));
    switch (PosVar)
    {
        case "Tit" : setBtnVar.setCompoundDrawables(null,DevDmjVar,null,null); break;
        case "Rit" : setBtnVar.setCompoundDrawables(null,null); break;
        case "Pit" : setBtnVar.setCompoundDrawables(null,DevDmjVar); break;
        default: setBtnVar.setCompoundDrawables(DevDmjVar,null); break;
    }
}

我如何在swift for iphone中做到这一点?

setBtnVar.setimage(<image: UIImage?>,forState: <UIControlState>)

解决方法

UPD:另见 UseYourLoaf blog post

刚刚在Erica Sadun blog post上找到iOS 11上的could use Vector Assets.

什么“矢量资产”意味着:

If you click that Box,the vector data will be shipped with your
application. Which,on the one hand,makes your application a little
bit larger,because the vector data takes up some space. But on the
other hand,will give you the opportunity to scale these images,which
might be useful in a number of different situations. So,one is,if
you kNow that this particular image is going to be used at multiple
sizes. But that might be less obvIoUs. So,one case is a symbolic
glyph that should resize with dynamic type. Since we’re thinking about
dynamic type,you should also be thinking about having glyphs that are
appearing next to type resize appropriately. Another case that’s
really not obvIoUs,is tab bar images.
… there’s a really great accessibility feature that we strongly
recommend supporting,that allows for user that have turned their
dynamic type size up. … So,we really recommend doing that to increase the usability of your app across all users

如何使用:

>将您的SVG文件转换为PDF,例如在ZamZar.com
>将您的pdf添加到Assets.xcassets

>为导入的pdf单击“保留矢量数据”.

>在UIViewController中创建UIImageView并分配像UIImage这样的pdf文件.

iOS< 11 没有本地方式来使用SVG图像. 看看Macaw

通过Cocoapod导入框架

pod "Macaw","0.8.2"

检查他们的example project:这是渲染tiger.svg的方法(位于项目目录中,而不是在Assets.xcassets文件中)

import UIKit
import Macaw

class SVGExampleView: MacawView {

    required init?(coder aDecoder: NSCoder) {
        super.init(node: SVGParser.parse(path: "tiger"),coder: aDecoder)
    }

}

当然还有其他一些第三方库:

> SwiftSVG
> Snowflake
> SVGKit Objective-C框架

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...