2013-03-26 61 views
0

我試圖複製Safari瀏覽器頁面滾動視圖,它的框架是比iPhone屏幕稍窄。所以根據一些建議,我在「clipView」(450x320點)內放置了滾動視圖(450x280點)。ios-用UIView容器內置UIScrollView與Autolayout編程

//viewController methods 
-(void)viewDidAppear:(BOOL)animated{ 

CGRect pagingScrollViewFrame = self.clipview.frame; 
pagingScrollViewFrame.size.width = 280; 

NSLog(@"clipview h:%f e w:%f",self.clipview.frame.size.height,self.clipview.frame.size.width); 
NSLog(@"scrollview h:%f e w:%f",pagingScrollViewFrame.size.height,pagingScrollViewFrame.size.width); 
NSLog(@"center clipview :%@",NSStringFromCGPoint(self.clipview.center)); 

pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame]; 
NSLog(@"center scrollview:%@",NSStringFromCGPoint(pagingScrollView.center)); 
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width *3, pagingScrollViewFrame.size.height); 

pagingScrollView.clipsToBounds = NO; 
pagingScrollView.center = self.clipview.center; 

    [clipview addSubview:pagingScrollView]; 
} 

enter image description here

正如你可以看到我有我的滾動視圖的中心,這是轉移20points下來({140} 232)。 我的視圖容器的中心位於{160,232}

我錯在哪裏?有什麼像我應該玩的內容偏移屬性?這個問題與Autolayout有關嗎?

回答

0

的問題是在這條線:

pagingScrollView.center = self.clipview.center; 

應該是:

pagingScrollView.center = CGPointMake(CGRectGetMidX(self.clipview.bounds), CGRectGetMidY(self.clipview.bounds)); 

這是常見的錯誤分配的上海華中心到其子視圖。他們在不同的座標系統中。

+0

現在,當我NSlog中心看起來他們有相同的座標{160,232},但是當我運行應用程序時,滾動視圖仍然向下移動20px。 – luca 2013-03-26 22:11:18