2012-01-17 71 views
1

我有一個UIScrollView,當我調整寬度時,我希望框架的高度可以按比例調整,這可能嗎?基本上我說的是當我調整UIScrollView的寬度時,自動調整UIScrollView的框架高度?我曾嘗試autoResizingMask設置爲UIViewAutoresizingFlexibleWidth和高度,但這不起作用調整寬度時UIScrollView的高度調整

+0

你是如何「調整寬度」滾動視圖的? –

+0

scrollView.frame.size.width – adit

回答

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 

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; 
+0

這就是我所做的,但想知道是否有自動方式,而不必這樣做 – adit

+0

以及您可以應用CGAffineTransformScale,但除此之外,我不這麼認爲。 –