How do I scale a UIButton's imageView?

前端 未结 19 2930
刺人心
刺人心 2020-12-02 05:16

I created a UIButton instance named \"button\" with an image using [UIButton setImage:forState:]. The button.frame is larger than the image\'s size.

Now

19条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 06:07

    From small test I just did after reading here, depends if you use setImage or setBackgroundImage, both did the same result and strech the image

    //for setBackgroundImage
     self.imageButton.contentMode = UIViewContentModeScaleAspectFill;
            [self.imageButton setBackgroundImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal];
    
    //for setImage
     self.imageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
            self.imageButton.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [self.imageButton setImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal];
    

提交回复
热议问题