【问题标题】:UIButton won't go to Aspect Fit in iPhoneUIButton 不会转到 iPhone 中的 Aspect Fit
【发布时间】:2011-03-28 12:05:47
【问题描述】:

我有几个 UIButton,在 IB 中它们设置为 Aspect Fit,但由于某种原因,它们总是在拉伸。你还有什么需要设置的吗?我尝试了所有不同的视图模式,但它们都不起作用,它们都是拉伸的。

【问题讨论】:

  • @Werner Altesischer 的答案是正确的。进行适当的更改。
  • @marty 你是在设置背景图片还是图片?背景图片不支持,只有图片。

标签: ios iphone uibutton aspect-fit


【解决方案1】:

解决方案是在UIButtonimageView 属性上设置contentMode。我相信UIButton 必须使用自定义类型创建才能正常工作(否则此属性会返回nil)。

【讨论】:

  • 这对我有用:[button.imageView setContentMode:UIViewContentModeScaleAspectFit];
  • 这也适用于我,但如果我设置 adjustsImageWhenHighlighted=YES,当我点击按钮时,图像会再次拉伸。
  • iOS 6 中已经消除了adjustsImage 中的拉伸。
  • 在 iOS 8 中,这实际上对我不起作用。原始答案(将 imageView 放在透明按钮后面)效果最好。
  • in Swift du jour:button.imageView!.contentMode = UIViewContentMode.scaleAspectFit
【解决方案2】:

这个方法对我很有效。:

Xib中选择按钮并设置user defined runtime attributes

  • 密钥路径:self.imageView.contentMode
  • 类型:Number
  • 值:1

我们使用数字是因为你不能在那里使用枚举。但是 UIViewContentModeScaleAspectFit 等于 1。

【讨论】:

  • 这也在设计时更新了界面构建器中的图像。很好的解决方案。
  • 很好的解决方案,代码很少,工作起来很迷人,枚举基于内容模式列表项。
  • 你也可以添加一个扩展来让它更简单一点:extension UIButton { @IBInspectable var imageContentMode: Int { get {return imageView?.contentMode.rawValue ?? 0} set {imageView?.contentMode = UIViewContentMode(rawValue: newValue) ?? .scaleAspectFit} } }
  • 没有“自我”imageView.contentMode 为我工作!
【解决方案3】:

我以前也遇到过这个问题。我通过将我的图像放入 UIImageView 来解决这个问题,其中 contentMode 设置实际上可以工作,并在其上放置一个透明的自定义 UIButton

编辑:这个答案已经过时了。请参阅@Werner Altewischer's answer 了解现代版本 iOS 中的正确答案。

【讨论】:

  • 是的,试过了,但现在无法让按钮重新适应图像
  • 如何使用这种技术管理按钮状态?似乎响应“突出显示”之类的用户交互将是一场噩梦。
  • 这可能是 2.5 年前回答此问题时的正确方法,但现在您可以编辑 UIButton 的内部 ImageView 并在那里设置内容模式。查看@Werner Altewischer 的答案
【解决方案4】:

如果您在 Interface Builder 中执行此操作,则可以使用运行时属性检查器直接进行设置,无需任何代码。

将按钮上的 Key Path 设置为“imageView.contentMode”,类型为“Number”,值为“1”(或您想要的任何模式)。

【讨论】:

    【解决方案5】:

    将按钮的 imageView 用于 contentMode。不是直接在按钮本身上。

    homeButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
    [homeButton setImage:[UIImage imageNamed:kPNGLogo] forState:UIControlStateNormal];
    

    【讨论】:

      【解决方案6】:

      如果您将图像放在按钮后面的UIImageView 中,您将失去UIButton 类的内置功能,例如adjustsImageWhenHighlightedadjustsImageWhenDisabled,当然还有能够为不同的状态设置不同的图像(不用自己做这件事)。

      如果我们想要对所有控制状态都未拉伸的图像,一种方法是使用imageWithCGImage:scale:orientation 获取图像,方法如下:

      - (UIImage *) getScaledImage:(UIImage *)img insideButton:(UIButton *)btn {
      
          // Check which dimension (width or height) to pay respect to and
          // calculate the scale factor
          CGFloat imgRatio = img.size.width / img.size.height, 
                  btnRatio = btn.frame.size.width / btn.frame.size.height,
                  scaleFactor = (imgRatio > btnRatio 
                                 ? img.size.width / btn.frame.size.width
                                 : img.size.height / btn.frame.size.height;
      
          // Create image using scale factor
          UIImage *scaledImg = [UIImage imageWithCGImage:[img CGImage]
                                                   scale:scaleFactor
                                             orientation:UIImageOrientationUp];
          return scaledImg;
      }
      

      为了实现这一点,我们将编写:

      UIImage *scaledImg = [self getScaledImage:myBtnImg insideButton:myBtn];
      [myBtn setImage:scaledImg forState:UIControlStateNormal];
      

      这应该可以防止图像在所有控制状态下拉伸。它对我有用,但如果没有,请告诉我!

      注意:这里我们正在解决与UIButton 相关的问题,但insideButton: 也可能是insideView:,或者任何想要将图像放入其中的东西。 p>

      【讨论】:

        【解决方案7】:

        我前段时间遇到过这个问题。我遇到的问题是我试图将此效果应用于有限的背景 UIButton,因此意味着您无法轻松调整它。

        诀窍是将其设置为仅图像,然后应用@ayreguitar 的技术,这应该可以解决它!

        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [myButton setContentMode:UIViewContentModeScaleAspectFill];
        [myButton setImage:@"myImage.png" forState:UIControlStateNormal];
        

        【讨论】:

          【解决方案8】:

          这对我有用

          [button.imageView setContentMode:UIViewContentModeScaleAspectFit];
          

          感谢@ayreguitar 的评论

          【讨论】:

            【解决方案9】:

            这是 swift 中的另一种答案:

            myButton.imageView?.contentMode = .ScaleAspectFit
            

            【讨论】:

              【解决方案10】:

              将几个不同的答案组合成一个解决方案——创建一个具有自定义类型的按钮,设置按钮的 imageView contentMode 属性,并设置按钮的图像(不是背景图像,它仍会缩放以填充)。

              //Objective-C:
              UIImage *image = [UIImage imageNamed:"myImageName.png"];
              UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
              [button setImage:image forState:UIControlStateNormal];
              button.imageView.contentMode = UIViewContentModeScaleAspectFit;
              
              //Swift:
              let image = UIImage(named: "myImageName.png")
              let button = UIButton(type: .custom)
              button.imageView?.contentMode = .scaleAspectFit
              button.setImage(image, for: .normal)
              

              【讨论】:

                【解决方案11】:
                btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
                

                【讨论】:

                • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。 - From review
                【解决方案12】:

                此答案基于@WernerAltewischer's answer

                为了避免将我的按钮连接到 IBOutlet 以在其上执行代码,我对 UIButton 的类进行了子类化:

                // .h
                @interface UIButtonWithImageAspectFit : UIButton
                @end
                
                // .m
                @implementation UIButtonWithImageAspectFit
                
                - (void) awakeFromNib {
                    [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
                }
                
                @end
                



                现在在您的 xib 中创建一个自定义按钮,并设置其图像(不是背景图像):


                然后,设置它的类:

                你已经完成了!
                而不是

                您的按钮的图像宽高比现在符合预期:

                【讨论】:

                  【解决方案13】:

                  iOS 12。 您还需要添加内容对齐方式

                  class FitButton: UIButton {
                  
                      required init?(coder aDecoder: NSCoder) {
                          super.init(coder: aDecoder)
                  
                  
                      }
                  
                      override func layoutSubviews() {
                          self.imageView?.contentMode = .scaleAspectFill
                          self.contentHorizontalAlignment = .fill
                          self.contentVerticalAlignment = .fill
                          super.layoutSubviews()
                  
                  
                      }
                  
                  }
                  

                  【讨论】:

                    【解决方案14】:

                    我遇到了图像没有按比例调整大小的问题,所以我修复它的方法是使用边缘插图。

                    fooButton.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
                    

                    【讨论】:

                      【解决方案15】:

                      UIView 内容模式适用于对应的 CALayer 的“内容”。这适用于UIImageViews,因为他们将CALayer 内容设置为对应的CGImage

                      drawRect: 最终渲染到图层内容。

                      自定义UIButton(据我所知)没有内容(圆角矩形样式按钮可能使用内容呈现)。该按钮具有子视图:背景UIImageView、图像UIImageView 和标题UILabel。在子视图上设置 contentMode 可能会做你想做的事,但搞乱UIButton 视图层次结构有点禁忌。

                      【讨论】:

                        【解决方案16】:

                        更改 UIButton.imageView.contentMode 对我不起作用。
                        我通过将图像设置为“背景”属性解决了这个问题。
                        如果需要,您可以添加比率约束

                        【讨论】:

                          【解决方案17】:

                          这与许多其他答案重叠,但我的解决方案是

                          • 将按钮的UIImageViewcontentMode 设置为.ScaleAspectFit - 这可以在Interface Builder 的“用户定义的运行时属性”中完成(即self.imageView.contentMode,数字,1)或在UIButton 子类;
                          • 禁用“自动调整子视图大小”;
                          • 将“Edge”设置为“Image”,并为“Inset”设置适当的“Top”和“Bottom”值(仅当您像我一样使用 PDF 作为图像时才需要这样做)。

                          【讨论】:

                            【解决方案18】:

                            您可以使用 imageWithCGImage 如上所示(但不要缺少括号)。

                            另外...数以百万计的非 4.0 手机根本无法使用该代码。

                            【讨论】:

                              【解决方案19】:

                              [button sizeToFit] 为我工作。

                              【讨论】:

                                【解决方案20】:

                                与@Guillaume 类似,我创建了 UIButton 的子类作为 Swift 文件。然后在 Interface Builder 中设置我的自定义类:

                                .

                                这里是 Swift 文件:

                                import UIKit
                                
                                class TRAspectButton : UIButton {
                                    required init?(coder aDecoder: NSCoder) {
                                        super.init(coder: aDecoder)
                                        self.imageView?.contentMode = .ScaleAspectFit
                                    }
                                }
                                

                                【讨论】:

                                  【解决方案21】:

                                  我遇到了同样的问题,但我无法让它工作(可能是 SDK 的一个错误)。

                                  最终,作为一种解决方法,我在按钮后面放置了一个 UIImageView 并设置了我想要的选项,然后简单地在其顶部放置了一个空白 UIButton

                                  【讨论】:

                                  • 这里有回声吗? ;-)
                                  猜你喜欢
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2011-03-17
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2016-12-30
                                  • 1970-01-01
                                  相关资源
                                  最近更新 更多