ios - UISegmentedControl 背景图片奇怪的行为
<p><p>我正在开发一个 iOS 项目,遇到了这个奇怪的问题。我有一个 <code>bar</code> 样式的 <code>UISegmentedControl</code> 元素。它有 3 个部分。这些片段的固定大小为 80。我还有 3 张图像,每个片段都被选为背景。这是其中之一`</p>
<p> <img src="/image/XwL6X.png" alt="enter image description here"/> </p>
<p>当 View 被加载时,其中一个片段被设置为选中,这个图像被设置为背景,如下所示:</p>
<pre><code> forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
</code></pre>
<p>现在,当我在 5.1 模拟器上运行项目时,我得到了分段按钮的扭曲 View :</p>
<p> <img src="/image/CmTjS.png" alt="enter image description here"/> </p>
<p>如您所见,中间部分保持我给出的大小,但图像从中间到边缘有点拉伸(stretch)。
即使我已将其设置为 <code>NO</code>,所选段也会突出显示。
在监听按钮选择的方法中,我更改了背景图像并得到以下信息:
<img src="/image/3eoeU.png" alt="enter image description here"/> </p>
<p>(背景图又被拉伸(stretch)了……)</p>
<p>iOS6 上的情况几乎相同。
现在是我做错了什么,还是有办法解决这个问题?</p>
<p><strong>编辑</strong></p>
<p>找到了一个更好的实现,它为普通按钮、选定按钮和分隔符使用单独的图像。请参阅下面的答案。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>似乎设置完整的背景图像不是处理自定义分段按钮的最佳方式。找到示例 <a href="http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5" rel="noreferrer noopener nofollow">here</a> .</p>
<p>这是完成所有工作的基本代码:</p>
<pre><code>UIImage *segmentSelected =
[
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
UIImage *segmentUnselected =
[
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
UIImage *segmentSelectedUnselected =
;
UIImage *segUnselectedSelected =
;
UIImage *segmentUnselectedUnselected =
;
[ setBackgroundImage:segmentUnselected
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[ setBackgroundImage:segmentSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[ setDividerImage:segmentUnselectedUnselected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[ setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[
setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
</code></pre></p>
<p style="font-size: 20px;">关于ios - UISegmentedControl 背景图片奇怪的行为,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15111300/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15111300/
</a>
</p>
页:
[1]