开发者

Resize UIImageView or other control in interfacebuilder using percentage

I have an UIImageView in interface builder and its current size is 100px x 100px. I know how to resize this by using resize handles at corners and by attribute inspector.

What I want it to resize it exact to a fix percent for eg. 50% so that the new size would automatically 50px x 50px.

I have many other images and don't want to calculate pixel percentage manually hence I need some wa开发者_高级运维y to get it done automatically.

Is there a way to do this?

Thanks,


I don't think there is a framework provided API for this. You can use categories to deal with this.

@interface UIImageView (Resizing)
- (void)resizeToPercentSizeOfOriginal:(CGFloat)percentage; // any value between 0 and 100
@end

@implementation UIImageView (Resizing)

- (void)resizeToPercentSizeOfOriginal:(CGFloat)percentage {
    if ( percentage < 0 || percentage > 100.0 ) {
        // Deal with it.
    }

    CGSize currentSize = self.frame.size;
    CGSize expectedSize = CGSizeMake(currentSize.width * percentage, currentSize.height * percentage);
    CGRect theFrame = self.frame;
    frame.size = expectedSize;
    self.frame = theFrame;
}

@end

You can put this in UIImageView+Resizing.h and UIImageView+Resizing.m and import the header to use the method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜