UIButton doesn't listen to content mode setting?

后端 未结 16 2038
情话喂你
情话喂你 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:26

    Only solution which worked for me:

    [button setImage:image forState:UIControlStateNormal];
    button.imageView.contentMode = UIViewContentModeScaleAspectFill;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
    
    0 讨论(0)
  • 2020-12-02 09:30

    Found a fix for this. Set the adjustsImageWhenHighlighted property of UIButton to NO.

      UIButton *b = [[UIButton alloc] initWithFrame:rect];
            [b setImage:image forState:UIControlStateNormal];
            [b.imageView setContentMode:UIViewContentModeScaleAspectFill];
            [b setAdjustsImageWhenHighlighted:NO];
    

    Hope this helps. Feel free to comment below, I will follow up on any questions that you have.

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

    Swift 3

    self.firstButton.imageView?.contentMode = .scaleAspectFill
    
    0 讨论(0)
  • 2020-12-02 09:32

    Instead of setImage try setBackgroundImage

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