2013-08-21 22 views
0

我目前有一個視圖,其中有一個scrollView。在那個scrollView上,我有一個按鈕和一個UITableView。在滾動視圖中移動按鈕 - iOS

鑑於某些條件,我想將這兩件事情提出來。我目前正在這樣做:

tempFrame = [addressTableView frame]; 
    tempFrame.origin.x = 0.0; 
    tempFrame.origin.y = 2.0; 
    tempFrame.size.width = 320.0; 
    tempFrame.size.height = 103.0; 
    [addressTableView setFrame:tempFrame];   
    // [self.scrollView addSubview:self.addressTableView]; (does not do anything) 

    tempFrame = [buttonContinue frame]; 
    tempFrame.origin.x = 20.0; 
    tempFrame.origin.y = 40.0; 
    tempFrame.size.width = 150.0; 
    tempFrame.size.height = 25.0; 
    [buttonContinue setFrame:tempFrame]; 
    // [self.scrollView addSubview:self.buttonContinue]; (does not do anything) 

此方法適用於沒有scrollView的視圖。

關於如何在scrollView中正確移動對象的任何提示將不勝感激。

謝謝。

回答

1

如果您只是移動它們,而不是調整大小,請嘗試使用CGAffineTransform移動按鈕。 (您也可以通過連接CGscale和CGtranslate來調整大小)。

因此,對於您的按鈕,那將是一件很接近:

buttonContinue.transform = CGAffineTransformMakeTranslation(20,40); 

這將移動您的按鈕20pixels的權利和40個像素下來。拋開

buttonContinue.transform = CGAffineTransformIdentity; 

此外,作爲,你可以創建一個新的框架,用一條線,而在四大行像上面這樣:

要稍後將其移動回只需撥打

buttonContinue.frame = CGRectMake(20, 40, 150, 25); 
+0

感謝您的答覆。不幸的是,這不起作用。我嘗試了兩種方法。我認爲這可能與這個按鈕和表視圖位於「視圖」內的scrollView上有關。但請注意,這兩者的「引用插座」都是文件的所有者。 – FidelCashflo

+0

你的按鈕是否連接到他們的IBOutlets(並且是那些IBOutlets屬性?) – Fluffhead

+0

是的,它們被連接起來,它們也是屬性。 – FidelCashflo

2

如果我理解你的問題,我認爲你應該取消選中文件檢查器中的「Autolayout」屬性來查看這個特定的ViewController。我有同樣的問題,並通過取消自動佈局我的問題解決了。可能是這將有助於解決你的問題。

祝您好運!

+0

感謝您的迴應。這確實奏效。但是取消選中自動佈局會使scrollView屏幕變得混亂。如同它將所有東西推到屏幕的頂部和背面。我需要重新設置scrollView的大小嗎? – FidelCashflo

+0

我認爲沒有其他辦法,所以你必須重置一切:( –

0

嘗試了這一點,

[addressTableView setFrame:CGRectMake(0,2,320,103)];   
[buttonContinue setFrame:CGRectMake(20,40,150,25)]; 

如果不工作再有就是對你的元素addressTableViewbuttonContinue參考的一個問題。如果它們不在您的滾動視圖中,請使用

[self.scrollView addSubview:addressTableView]; 
[self.scrollView addSubview:buttonContinue]; 

確保您的scrollView變量與IB上的元素相鏈接。

0

最好的方式來移動按鈕

[btn addTarget:self action:@selector(pointMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
-(IBAction) pointMoved:(id) sender withEvent:(UIEvent *) event 
{ 
    CGPoint point = [[[event allTouches] anyObject] locationInView:self]; //here you can give scrollview 
    UIControl *control = sender; 
    control.center = point; 
}