2013-04-16 84 views
1

我有一個自定義的UIView子類需要在超級視圖的底部。我將使用視圖的起源:直接Superview中的自定義UIView放置

CGRect subviewFrame = subview.frame; 

CGPoint newOrigin = CGPointMake(0, superview.bounds.size.height - subviewFrame.size.height); 

subviewFrame.origin = newOrigin; 

[subview setFrame:subviewFrame]; 

然而,這個地方的子視圖(origin.y)的上海華的觀點框架之外。

Subview outside of frame

如果我使用:

CGPoint newOrigin = CGPointMake(0, superview.bounds.size.height - subviewFrame.size.height * 2.0f); 

我得到的結果我想,這是子視圖坐在窗口的底部。

Subview inside of frame

我不明白爲什麼我有2

乘以子視圖的高度。如果有人能告訴我,我錯過了什麼,將不勝感激。謝謝!

+0

圖片如何顯示您的意思。我的代碼沒有看到任何錯誤。 –

+0

當然...抱歉的大小。謝謝! –

+2

我確定它與navBar有關。而不是乘以2你的看法,減去44.0,然後減去你的子視圖。我相信你會得到你想要的東西,並明白爲什麼。 –

回答

1

我正在使用[UIScreen mainScreen] .bounds來獲取初始幀,它不會補償狀態和導航欄。 UIScreen applicationFrame:返回框架減去狀態欄高度。我使用這種方法並考慮導航欄高度(44.0)以獲得所需的結果。

1

繪製UIView邊界的有用代碼。 KADebugShowViewBounds(superview,[UIColor redColor])和KADebugShowViewBounds(子視圖,[UIColor redColor])。你會看到紅色的superview和子視圖的界限。我認爲這個問題應該是你的佈局。

#import <Foundation/Foundation.h> 
#import <QuartzCore/QuartzCore.h> 

#ifndef KAViewDebugHelper_h 
#define KAViewDebugHelper_h 

#ifdef DEBUG 
#define KADebugShowViewBounds(aView, aColor) \ 
do \ 
{ \ 
    UIColor *color = [(id)aColor isKindOfClass:[UIColor class]] ? aColor : [UIColor redColor]; \ 
    aView.layer.borderColor = color.CGColor; \ 
    aView.layer.borderWidth = 1.0f; \ 
} \ 
while(0) 
#else 
#define KADebugShowViewBounds(aView) 
#endif 
#endif