Replacement for UIView's contentStretch?

2019-02-10 19:23发布

In iOS 6.0 the contentStretch property of UIView was deprecated. How can I achieve the same functionality with new/old/other API?

The functionality I am looking for is stretching just part of an image (everything but the edges) on a UIButton.

1条回答
成全新的幸福
2楼-- · 2019-02-10 19:53

Use resizableImageWithCapInsets and UIEdgeInsets to make a stretching UIButton background image. For example:

UIImage* image = [UIImage imageNamed:@"image.png"];
image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)];
[button setBackgroundImage:image forState:UIControlStateHighlighted];

I don't think you can stretch more than one pixel both horizontal and vertical.

Hope this helps.

查看更多
登录 后发表回答