<bdo id='ROt6F'></bdo><ul id='ROt6F'></ul>
    <legend id='ROt6F'><style id='ROt6F'><dir id='ROt6F'><q id='ROt6F'></q></dir></style></legend>
      <tfoot id='ROt6F'></tfoot>

    1. <i id='ROt6F'><tr id='ROt6F'><dt id='ROt6F'><q id='ROt6F'><span id='ROt6F'><b id='ROt6F'><form id='ROt6F'><ins id='ROt6F'></ins><ul id='ROt6F'></ul><sub id='ROt6F'></sub></form><legend id='ROt6F'></legend><bdo id='ROt6F'><pre id='ROt6F'><center id='ROt6F'></center></pre></bdo></b><th id='ROt6F'></th></span></q></dt></tr></i><div id='ROt6F'><tfoot id='ROt6F'></tfoot><dl id='ROt6F'><fieldset id='ROt6F'></fieldset></dl></div>

        <small id='ROt6F'></small><noframes id='ROt6F'>

        如何在 UIButton 中居中图像而不在 Swift 中双向拉伸?

        时间:2023-07-07

        <small id='b0qPx'></small><noframes id='b0qPx'>

          • <legend id='b0qPx'><style id='b0qPx'><dir id='b0qPx'><q id='b0qPx'></q></dir></style></legend>

            1. <tfoot id='b0qPx'></tfoot>
                <tbody id='b0qPx'></tbody>
              <i id='b0qPx'><tr id='b0qPx'><dt id='b0qPx'><q id='b0qPx'><span id='b0qPx'><b id='b0qPx'><form id='b0qPx'><ins id='b0qPx'></ins><ul id='b0qPx'></ul><sub id='b0qPx'></sub></form><legend id='b0qPx'></legend><bdo id='b0qPx'><pre id='b0qPx'><center id='b0qPx'></center></pre></bdo></b><th id='b0qPx'></th></span></q></dt></tr></i><div id='b0qPx'><tfoot id='b0qPx'></tfoot><dl id='b0qPx'><fieldset id='b0qPx'></fieldset></dl></div>
                <bdo id='b0qPx'></bdo><ul id='b0qPx'></ul>
                  本文介绍了如何在 UIButton 中居中图像而不在 Swift 中双向拉伸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在制作一个自定义键盘,我需要在 shift 按钮内居中显示 shift 图标的图像.图像为 100 像素 x 100 像素.

                  I am making a custom keyboard and I need to centre the image of the shift icon inside the shift button. The image is 100px x 100px.

                  但是,纵向的按钮框架是36pt x 38pt,横向是68pt x 32pt.我尝试使用按钮图像插入属性来使图像居中.但是,它没有这样做.

                  However, the button frame in portrait is 36pt x 38pt, and in landscape, it is 68pt x 32pt. I have tried using button image inset property to centre the image. However, it does not does so.

                  我自己尝试并将图像边缘插入设置为 (25,25,25,25),图像以两种尺寸居中.但是,图标变得太小而无法看到.

                  I tried myself and set the image edge insets to (25,25,25,25), the image centres in both sizes. However, the icon becomes too small to see.

                  shiftButton.setImage(UIImage(named: "shift"), forState: .Normal)
                  
                  shiftButton.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0)
                  

                  我正在以编程方式制作按钮.而且,我需要按钮来缩放保持其纵横比,即 1:1.它的最大宽度或高度总是与按钮本身的高度相关联.图像宽度/高度应为按钮高度的最大值.而且,它不应该以任何方式拉伸以扭曲图标.但是,按钮本身可以拉伸.

                  I am making the button programatically. And, I need the button to scale maintaining its aspect ratio i.e. 1:1. Its maximum width or height is always tied to the button's height itself. The image width/height should be maximum of button's height. And, it should not stretch in any way to distort the icon. However, the button itself can stretch.

                  我需要根据按钮框架缩放图像,但要保持其纵横比.而且,它可以缩小或放大,但应始终保持 1:1 的纵横比.

                  I need the image to scale according to button frame but maintain its aspect ratio. And, it can down scale or up scale, but should always maintain the 1:1 aspect ratio.

                  对解决这个问题有什么建议吗?

                  Any suggestions for solving this problem?

                  推荐答案

                  终于找到了解决方案.您必须为 UIButton 内的图像设置内容模式.

                  Finally a solution came through. You must set the content mode for the image inside the UIButton.

                  解决方案是在按钮内使用前景图像时更新 ImagecontentMode 以及UIButton.

                  The solution is to update the contentMode of the Image when you are using a foreground image inside a button along with the contentMode of the UIButton.

                  shiftButton.contentMode = .Center
                  shiftButton.imageView?.contentMode = .ScaleAspectFit
                  

                  这篇关于如何在 UIButton 中居中图像而不在 Swift 中双向拉伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:动画 UIButton 的标题更改 下一篇:如何在禁用状态下更改 UIButton 图像 alpha?

                  相关文章

                    <tfoot id='8Gb3d'></tfoot>

                      <small id='8Gb3d'></small><noframes id='8Gb3d'>

                    1. <legend id='8Gb3d'><style id='8Gb3d'><dir id='8Gb3d'><q id='8Gb3d'></q></dir></style></legend>
                        <bdo id='8Gb3d'></bdo><ul id='8Gb3d'></ul>

                      <i id='8Gb3d'><tr id='8Gb3d'><dt id='8Gb3d'><q id='8Gb3d'><span id='8Gb3d'><b id='8Gb3d'><form id='8Gb3d'><ins id='8Gb3d'></ins><ul id='8Gb3d'></ul><sub id='8Gb3d'></sub></form><legend id='8Gb3d'></legend><bdo id='8Gb3d'><pre id='8Gb3d'><center id='8Gb3d'></center></pre></bdo></b><th id='8Gb3d'></th></span></q></dt></tr></i><div id='8Gb3d'><tfoot id='8Gb3d'></tfoot><dl id='8Gb3d'><fieldset id='8Gb3d'></fieldset></dl></div>