how to get coordinates of button with respect to superview? - ios

My view hierarchy is
UIViewController.view
a subview (on main view with half size and in bottom)
a button on this subview
I want to get the co-ordinates of this button with respect to my main view
the CGRect of this button but according to superview.

[self.view convertRect:button.frame fromView:button.superview];

use
[view convertPoint:thePoint toView:subview];

Related

Showing UIView back in same frame after removeFromSuperView

I am using an UIView as container for UIButton. When user opens keyboard, I am removing from superview and adding in to TextField AccessoryView for sticky button design.
[_buttonView removeFromSuperview];
[_usernameTextField setInputAccessoryView:_buttonView];
But after that I add view back the view show top of the screen. I Set its frame but, it is also not working.
_buttonView.frame = CGRectMake(0, 561, 320, 106);
[self.view addSubview:_buttonView];
So I know that, removeFromSubView method removes view from memory, so frame is not stored. If I don't removeFromSubView AccessoryView is not adding it because of parent child issue.

Add subView in storyboard

In my program needs to create view, then create subView and add it in view.
I create view and subView in storyboard. Then in code:
[subView removeFromSuperview];
[view addSubview:subView];
[self.view addSubview:view];
How can I add subView to view in storyboard without code?
You don't actually need to write any code for this. There are many ways to do this:
Select the subview and drag over the view entry in the Objects Explorer of storyboard. Once you leave the mouse hold, the parent view will have a triangle indicating your subview has become its child and the subview will have a bigger indent than your view
Or you can use the "Embed in View" menu as shown in the below pic [source:http://codesheriff.blogspot.co.il/2014/03/8-tips-for-working-effectively-with.html]
open file inspector ->search for view drag and Drop the view in storyboard
so in this you already holding a view now you adding an view its almost like adding a sub view to the main view this is for adding view without code
Open the storyboard and just create the new view inside the subview.
What your doing above is doing nothing.
[subView removeFromSuperview]; //Your removing subview from the superview
[view addSubview:subView]; //is view a new UIView?
[self.view addSubview:view];
You Will have a View in the bottom of right side.You Just drag and Drop the view Where you Want.To Set constriants clearly We are goin Dynamic view Creation.If you do View creation programmatically it will help you in the future project.

iOS 7 UIScrollView doesn't scroll when presented as modal view controller, works fine otherwise

I have a storyboard in which I have a view controller, (InfoViewController) in which I have an UIScrollView with some labels, uitextviews, etc. this is all created in IB, no code has been written at all. The only thing that is left for me to do is to set the content size, which I do as following:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGRect screenRect = [[UIScreen mainScreen] bounds];
[self.scrollView setContentSize:CGSizeMake(screenRect.size.width, 600)];
[self.scrollView setBackgroundColor:[UIColor greenColor]];
}
Whenever I make this view the entry point of my app, it works perfectly. I can see my view, the content size is set, the background color is being set to green.
Now it comes, I created another view controller, and this view controller is now my entry point of the app. I added a button in there, and on this button I did a "modal segue" to the earlier mentioned Info View Controller.
When I now run my app, I press this button, my Info View Controller shows up. The green background color is being set, but it's impossible to scroll. So the code is being executed (otherwise the background color couldn't been green, in the storyboard it's just plain white) but somehow whenever I use this "modal segue", the scroll functionality gets lost.
How can I fix this?
Try to insert a UIView into the scroll view...
Set the UIView with top, bottom, leading and trailing space to super view to 0.
Then insert everything into the UIView rather than into the ScrollView
Then modify the constraint height of the inner UIView instead of the contentsize of the scroll view, it works with iOS7

Drag a UIView from one UIScrollView to another

I have two UIScrollViews on my screen and I need to be able to drag a UIView from one scrollview to the other.
At the moment, I have a UILongGestureRecognizer on the UIView that I want to move, such that when the user starts dragging it, I make the view follow the touch:
- (void)dragChild:(UILongPressGestureRecognizer *)longPress {
[[longPress view] setCenter:[longPress locationInView:[[longPress view] superview]]];
}
But when I get the boundary of the starting UIScrollView, the view disappears because it's locked into that scrollview's bounds.
Is there a way to "pop" it out of the scrollview when I start dragging such that I can carry it over to the other scrollview?
Also, how do I test for the "drop" location? I want to know if it's been dropped over a certain other view.
Or am I going about this all the wrong way?
Thanks guys
If you will need to drag it from a scroll view to another do the following (pseudo code)
when you start dragging do the following
//scrollView1 is the scroll view that currently contains the view
//Your view is the view you want to move
[scrollView1.superView addSubView:yourView];
now when dropping the view, you will need to know if it is inside the other scrollview
//scrollView2 is the second scroll view that you want to move it too
//Your view is the view you want to move
CGPoint point = yourView.center;
CGRect rect = scrollView2.frame;
if(CGRectContainsPoint(rect, point))
{
//Yes Point is inside add it to the new scroll view
[scrollView2 addSubView:yourView];
}
else
{
//Point is outside, return it to the original view
[scrollView1 addSubView:yourView];
}
Here is an untested idea:
When the drag begins, move the dragging-view out of the scroll view (as a subview) and into the mutual superview of both scroll views.
When the drag ends, move the dragging-view out of the superview and into the new scroll view.
You'll probably have to be careful with coordinate systems, using things like [UIView convertPoint:toView:] to convert between the views' different perspectives when moving things around.

How to handle different scrollView for orientation change in iPad?

I have a scroll view with 2 different subviews for two different orientation.
In portait mode, it shows a table view and in portait it shows the UIImageView.
I want to know the ideal way to handle the orientation.
I am having glitches with my current way of handling
1.Load a view,and add scrollview as a subview to the root view.
2.Change orientation,
3.Change the frame and contents of the scrollview.
4.Add this modified scrollview as subview again.
But, every time I change orientation,the new view gets added on top of the previous one.
If you want to have two different views for different orientations, you need to switch them while orientation is changing.
When you go from portrait to landscape, you have to remove UIImageView and add scrollview (and vice versa)
[myImageView removeFromSuperview];
[self.view addSubview:scrollView];
You can also check if view doesn't have a superview to be sure that you have to add it:
if(scrollView.superview == nil)
[self.view addSubview:scrollView];
As you have already added scrollView as subView so you do not need to add it as subView again.
Just change the frame and contentSize of scrollView.
for Views do as
[scrollView.subView removeFromSuperView]
[scrollView addSubView:view1]; // or add view2

Resources