2013-08-21 24 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; 
}