UIButton doesn't listen to content mode setting?

后端 未结 16 2037
情话喂你
情话喂你 2020-12-02 08:45

firstButton is a UIButton of type Custom. I\'m programmatically putting three of them across each cell of a table, thusly:

[firstButton setImage:markImage fo         


        
相关标签:
16条回答
  • 2020-12-02 09:05

    I had the same problem. I see this question is a little old, but I want to provide a clear and correct answer to save other folks (like me) some time when it pops up in their search results.

    It took me a bit of searching and experimenting, but I found the solution. Simply set the ContentMode of the "hidden" ImageView that is inside the UIButton.

    [[firstButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
    [firstButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    

    Perhaps that's what Dan Ray was alluding to in his accepted answer, but I suspect not.

    0 讨论(0)
  • 2020-12-02 09:12

    These two things (which are quite hard to find initially) will stretch your UIButton image to fit the button size:

    one should always try to set such in the Storyboard rather than code.

    0 讨论(0)
  • 2020-12-02 09:14

    In trying to figure this out, my method got a bit hackier as time went on, and I wound up subclassing UIButton and overriding setHighlighted:

    For me it works to just knock down the image alpha to .5, because they're on a black background.

    However, it only works if I comment out [super setHighlighted:] (where it appears the image-stretchy code is going on), which just doesn't feel like the right way to solve this at all...everything seems to be working fine, though. We'll see how it holds up as I keep working on it.

    - (void)setHighlighted:(BOOL)highlight {
        if (highlight) {
            [self.imageView setAlpha:.5];
        }  else {
            [self.imageView setAlpha:1];        
        }
    
    //    [super setHighlighted:highlight];
    }
    
    0 讨论(0)
  • 2020-12-02 09:16

    Rather than setting the contentMode on the button itself, you'll want to set contentHorizontalAlignment and contentVerticalAlignment properties and (crucially) the contentMode for the button's imageView for any kind of aspect fill or fit. Here's an example:

    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    Of course, you can also do other things like aligning the button's image to the top of the button. If you don't need an aspect fill or fit, you just can set the alignment by itself:

    button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    

    In Swift, you'll want to use:

    button.contentHorizontalAlignment = .fill
    button.contentVerticalAlignment = .fill
    button.imageView?.contentMode = .scaleAspectFit
    

    This works for all versions of iOS, including the latest versions with auto-layout, (i.e. iOS 4 up to iOS 11+).

    0 讨论(0)
  • 2020-12-02 09:16

    If anyone looking for answer that work in iOS 6 and iOS 7 and storyboard:

    You can set image in your storyboard:

    enter image description here

    And then:

    for(UIView* testId in self.subviews) {
        if([testId isKindOfClass:[UIImageView class]])
            [testId setContentMode:UIViewContentModeScaleAspectFill];
    }
    
    0 讨论(0)
  • 2020-12-02 09:18

    I also advice to have a look at the adjustsImageWhenHighlighted UIButton property to avoid weird deformations of the image, when the button is pressed.

    0 讨论(0)
提交回复
热议问题