菜鸟教程小白 发表于 2022-12-11 17:55:18

ios - 如何在 iOS 和 WatchKit 中更改图像 tintColor


                                            <p><p>我有一个名为“theImageView”的 UIImageView,UIImage 是单一颜色的(透明背景),就像下面的左黑心一样。如何根据 iOS 7+ 导航栏图标中使用的 tint 方法在 iOS 7 或更高版本中以编程方式更改此图像的色调颜色?</p>

<p>此方法是否也适用于 Apple Watch 应用的 WatchKit?</p>

<p> <img src="/image/6GiCO.jpg" alt="enter image description here"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>iOS</strong><br/>
对于 iOS 应用,在 Swift 3、4 或 5 中:</p>

<pre><code>theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate)
theImageView.tintColor = UIColor.red
</code></pre>

<p>对于 Swift 2:</p>

<pre><code>theImageView.image = theImageView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
theImageView.tintColor = UIColor.redColor()
</code></pre>

<p>同时,现代的 Objective-C 解决方案是:</p>

<pre><code>theImageView.image = ;
];
</code></pre>

<hr/>

<p><strong>Watchkit</strong><br/>
在 Apple Watch 应用程序的 WatchKit 中,您可以设置 <a href="https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceImage_class/index.html#//apple_ref/occ/instm/WKInterfaceImage/setTintColor:" rel="noreferrer noopener nofollow">tint color for a template image</a> . </p>

<ol>
<li>您必须将图像添加到 WatchKit 应用中的 Assets 目录中,并在属性检查器中将图像集设置为呈现为模板图像。与 iPhone 应用不同的是,目前 WatchKit Extension 中无法在代码中设置模板渲染。</li>
<li>在应用的界面构建器中设置要在 WKInterfaceImage 中使用的图像</li>
<li>在 WKInterfaceController 中为名为“theImage”的 WKInterfaceImage 创建一个 IBOutlet...</li>
</ol>

<p>然后在 Swift 3 或 4 中设置色调颜色:</p>

<pre><code>theImage.setTintColor(UIColor.red)
</code></pre>

<p> swift2:</p>

<pre><code>theImage.setTintColor(UIColor.redColor())
</code></pre>

<p>然后在 Objective-C 中设置 tint 颜色:</p>

<pre><code>];
</code></pre>

<p>如果您使用模板图像并且未应用色调颜色,则会应用您的 WatchKit 应用的全局色调。如果没有设置全局色调,<code>theImage</code> 用作模板图像时默认为浅蓝色。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 和 WatchKit 中更改图像 tintColor,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40202163/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40202163/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 和 WatchKit 中更改图像 tintColor