调整宽度时调整UIScrollView高度

时间:2012-01-17 03:51:56

标签: iphone objective-c ipad uiscrollview

我有一个UIScrollView,我想在调整宽度时按比例调整框架高度,这可能吗?基本上我在谈论在调整UIScrollView的宽度时自动调整UIScrollView的框架高度?我已经尝试将autoResizingMask设置为UIViewAutoresizingFlexibleWidth和height,但这不起作用

2 个答案:

答案 0 :(得分:1)

我认为没有一种自动方式可以做到这一点。但你可以添加一个UIView + SCaleAddition来做到这一点:

@interface UIView(RespectScale)
- (void)setWidthRespect:(float)w;
- (void)setHeightRespect:(float)h;
@end

@implement UIView(RespectScale)
- (void)setWidthRespect:(float)w
{
   float scale = self.bounds.size.width / self.bounds.size.height;
   float h = w/scale;
   CGRect bounds = self.bounds
   bounds.size.width = w;
   bounds.size.height = h;
   self.bounds = bounds;
}

- (void)setHeightRespect:(float)h
{
  //  write it your own
}
@end

答案 1 :(得分:0)

你可以这样做:

//get old proportions
CGFloat proportion = scrollView.frame.size.width / scrollView.frame.size.height;

CGRect frame = scrollView.frame;

frame.size.width = new_width;
frame.size.height = new_width * proportion;

scrollView.frame = frame;