Reputation: 23
On iOS, what's a way to be able to place a view, like a subView, anywhere on screen (Main View)? I'm providing an interface to users where they can place it anywhere they want (Docking Views) ?
Upvotes: 0
Views: 702
Reputation: 688
Follow these two steps:
1). yourSubView.frame = CGRectMake(xOffset,yOffset,50,50);
2). [self.view addSubview:yourSubView];
DRAG AND DROP:
If u want to drag and drag the subviews u can make use of touchmethods:
to get the current x and y coordinates and fill them in the frame dynamically.
LIKE:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: touch.view];
yourSubview.frame = CGRectMake(location.x, location.y, 50, 50);
}
Upvotes: 0
Reputation:
myView.frame = CGRectMake(x, y, width, height);
Define x,y,w,h;
myView.frame = CGRectMake(50, 50, 100, 100);
Upvotes: 0
Reputation: 131491
I'd recommend using a pan gesture recognizer (UIPanGestureRecognizer) instead of using touchesBegan/touchesMoved/touchesEnded. It's much easier and cleaner.
Do a search on "Touches" in the Xcode docs and look for a sample application with that name. There are 2 projects included - one using gesture recognizers, and the other using touchesBegan/touchesMoved/touchesEnded.
It shows exactly what you need to do in order to drag a view around on the screen.
Upvotes: 0