I'm trying to get the offset of my viewControllers' views relative to the top of the screen.
I thought I could convert the origin of the view to the window's coordinates so I tried something like this in my viewControllers:
CGPoint basePoint = [self.view convertPoint:self.view.frame.origin toView:nil];
CGFloat offset = basePoint.y;
It works as expected in some cases, however in other cases it returns different values even when the parameters are the same.
Any ideas on what's going on behind the scenes of this convertPoint:toView: that might be resulting in different return values?
Or if you have any other suggestions to get the offset of my viewControllers' views relative to the top of the screen it would be very much appreciated!
Thanks
You are converting your point to a point in a nil view.
The initial window, has a rootViewController which has a view and so you can access to that view:
UIView *firstView = [[[(YourClassAppDelegate *)[UIApplication sharedApplication] window] rootViewController] view];
CGPoint basePoint = [self.view convertPoint:self.view.frame.origin toView:firstView];
CGFloat offset = basePoint.y;
remember to import in the implementation, your class of the app delegate.
I end up crawling up the chain of superviews and adding up all the offsets.y from each view. Not sure if it's the best approach, especially performance wise, but for now it works.
UIView *view = self.view;
CGFloat offset = view.frame.origin.y;
while (view.superview != nil) {
offset += view.superview.frame.origin.y;
view = view.superview;
}
try change this
CGPoint basePoint = [self.view convertPoint:self.view.frame.origin toView:nil];
to
CGPoint basePoint = [self.view.superview convertPoint:self.view.frame.origin toView:nil];
Your two views must have common superview (for example UIWindow), otherwise you'll get weird results.
This might happen when you try to use UIViewController's view in -viewDidLoad with convertPoint, because view is not yet added to the view hierarchy.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%#", self.view.superview); // nil, not added to view hierachy
// If you try to convert point using self.view (or any self.view subview),
// and any other view, which is not self.view subview (for example, UIWindow),
// it will fail, because self.view is not yet added to the view hierachy.
}
Related
The basic scenario is under everything, I have a map view in my view controller.
Above that, I animate an "annotationCreateView" on top of it. This view is already instantiated and moves between isHidden = No to isHidden = Yes and animates on and off screen accordingly.
However, I'm having trouble adding an annotation to the user's current location at the end of animating the view onscreen.
For some reason, the view will animate all the way up, and then upon executing the code where I add the annotation view, the view will disappear.
If I check for the IsHidden, it says no, meaning that it should be present. However, if I try to reanimate it up, it will slide all the way up and disappear again.
This is my code:
createAnnotationViewPresent = true;
[_annotationCreateView setHidden:NO];
[UIView animateWithDuration:1.0f animations:^{
CGRect frame = self.view.frame;
frame.size.height = frame.size.height/2;
frame.origin.y = frame.origin.y + frame.size.height;
_annotationCreateView.frame = frame;
} completion:^(BOOL finished) {
MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
point.coordinate = _mapView.userLocation.coordinate;
[_mapView addAnnotation:point];
self.point = point;
}];
Any suggestions appreciated, let me know if you need more code or information.
So the Problem I was facing was that Apple's auto layout constraints make it such that after the View reaches the end of the animation and then the pin for the map view is placed on, it reaches a conflict. It then decides to resolve this conflict by resetting my view to it's original position and made it seem like it was never moved in the first place. At least as far as I understand :P. The solution to my problem was to simply relieve the view controller's auto layout constraints and voila, problem solved.
I have a UIScrollView filled with draggable (UIView) cards as subviews and I want the cards to re-organize themselves (make space for new when the user drags one of them into the UIScrollView.
The problem is: how do I know which of the UIViews is under the one I'm dragging, so I can get its index and move it away from the card being dragged?
I tried using hitTest:withEvent: but I think I'm far from doing it right, since it's returning nil.
UIView *viewUnderCard = [card hitTest:card.center withEvent:nil];
Just started developing for iOS. Any help please?
You're on the right track, hitTest:withEvent: can be used. But #Mysiaq is correct that pointInside:withEvent: is probably even better.
You need to make sure that the coordinates are relative to the correct view. If you're using card.center, the coordinate system is that of the card's parent view.
The code could look something like this:
UIView *container = viewThatHasAllTheCards;
UIView *targetCard = nil;
CGPoint cardInWindow = [draggedCard.superview convertPoint:draggedCard.center toView:nil];
CGPoint cardInContainer = [container convertPoint:cardInWindow fromView:nil];
for (UIView *subview in container.subviews) {
if (subview == draggedCard) {
// Skip the dragged card.
continue;
}
if ([subview pointInside:cardInContainer withEvent:nil]) {
targetCard = subview;
// If you want the lower-most card, break here.
// If you want the top-most card, do not break here.
}
}
Get point of your touch and then call function
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
for all UIScrollView subviews and function will return you YES if provided CGPoint is inside of its frame.
You can compare bounding rects of two views:
CGRect boundsView1 = [view1 convertRect:view1.bounds toView:nil];
CGRect boundsView2 = [view2 convertRect:view2.bounds toView:nil];
Boolean viewsOverlap = CGRectIntersectsRect(boundsView1, boundsView2);
From here, you should be able to figure out how to iterate efficiently through your list of views to determine if any overlap.
I design the scrollview in interface builder like this
It looks good here. But unfortunately when I run it on emulator or device
it becomes
The content in scrollview is expand outside scrollview itself and even though outside UIView that contains this scrollView.
In my viewDidLoad (panel is the container of scrollView )
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CGFloat adjustPanelHeight = [PTTScreenScaleUtil getAdjustHeight:self.panel.frame.size.height];
CGRect panelRect = self.panel.frame;
panelRect.size.height = adjustPanelHeight;
self.panel.frame = panelRect;
UIImage *image = [UIImage imageNamed:#"panel-background"];
UIGraphicsBeginImageContext(self.panel.frame.size);
[image drawInRect:CGRectMake(0, 0, self.panel.frame.size.width, adjustPanelHeight)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.panel setBackgroundColor:[UIColor colorWithPatternImage:newImage]];
NSLog(#"scrollView Height : %f", self.scrollView.frame.size.height);
NSLog(#"scrollView contentSize Height : %f", self.scrollView.contentSize.height);
// CGRect scrollViewRect = self.scrollView.frame;
// CGRect scrollViewContentRect = self.scrollView.frame;
// NSLog(#"ScrollView Height Before : %f , After : %f", self.scrollView.frame.size.height, [PTTScreenScaleUtil getAdjustHeight:self.scrollView.frame.size.height]);
// scrollViewRect.size.width = 280;
// scrollViewRect.size.height = [PTTScreenScaleUtil getAdjustHeight:270];
// self.scrollView.frame = scrollViewRect;
// [self.detailsLabel sizeToFit];
UIView *view = [[self.scrollView subviews] objectAtIndex:0];
// [view sizeToFit];
// [self.scrollView setContentMode:UIViewContentModeScaleAspectFit];
// NSLog(#"ContentSize Height : %f", view.frame.size.height);
// scrollViewContentRect.size.height = view.frame.size.height;
NSLog(#"Bounds : %f", view.bounds.size.height);
self.scrollView.frame = CGRectMake(10, 10, 280, 270);
self.scrollView.contentSize = CGSizeMake(280, 500);
NSLog(#"Frame Height %f", self.scrollView.frame.size.height);
//[self.scrollView setContentSize: CGSizeMake(280, 1000)];
CGRect termBtnRect = self.termBtn.frame;
CGRect mailBtnRect = self.mailBtn.frame;
CGRect twitterBtnRect = self.twitterBtn.frame;
CGRect fbBtnRect = self.fbBtn.frame;
termBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
mailBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
twitterBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
fbBtnRect.origin.y = adjustPanelHeight - 10 - termBtnRect.size.height;
self.termBtn.frame = termBtnRect;
self.mailBtn.frame = mailBtnRect;
self.twitterBtn.frame = twitterBtnRect;
self.fbBtn.frame = fbBtnRect;
}
All the log return 270.0
PS. the scroll bar is correct even though the content goes outside but the scroll bar is working correctly (stay in the scrollview's frame as arrange in interface builder)
I have no idea how can I solve this.
Anyone help me please.
Thanks you.
Solve it by creating new view controller in interface builder and redo the same process with careful and bingo. It works.
When I compare both two view controller I realise that the wrong one UIScrollView Clip Subviews is unchecked. When check it the problem solve.
I just struggled with this for an hour and had a head smack moment.
In my case, I had a UIView on the scene in the Storyboard. At some point I decided I needed it to be a UIScrollView instead (as opposed to the original plan which was to embed the UIScrollView in a UIView)
I went ahead and changed the class on the UIView to UIScrollView. IB changed it to Scroll View in the Document Outline, I figure I'm good, right?
And then I see the behavior you describe.
At some point it hits me that this isn't sufficient. Apparently adding a UIScrollView via IB does some things differently than adding a UIView and just changing class isn't enough. And this is probably the reason re-doing it from scratch fixed it for you.
So for anyone who runs into this in the future, make sure you added the UIScrollView via IB instead of a UIView
I was having the same problem as described in this post. I tried multiple combinations of solutions that did not work, including:
putting the scroll view inside a view with Clip To Bounds = YES
putting a view inside the scroll view with Clip To Bounds = YES, that then contained my child view
putting a Container View inside the scroll view, and then embedding my subview
rebuilding the Interface Builder files completely
every combination of autosizing mask options systematcially for both the scroll view and container view
clip to bounds enabled or disabled for every single element systematically
The child view in question had previously worked inside a scroll view, but wouldn't in this one case where the content blew outside the bounds of the scroll view.
In the end, I implemented the solution in code as I could find no way to get Interface Builder to co-operate:
// There are two scroll areas on the screen, the left view and the right view.
// We want the right view to contain a scrollable area with another child view controller
// we designed in Interface Builder.
// create a scroll view to fill the right view with a scrollable area
CGSize rightFrameSize = self.rightView.bounds.size;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake (0, 0, rightFrameSize.width, rightFrameSize.height)];
scrollView.contentSize = CGSizeMake(640, 1352);
[self.rightView addSubview:scrollView];
// now create our child view controller from Interface Builder and add it to the scroll view
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"CustomerAddress" bundle:[NSBundle mainBundle]];
detailsView = [sb instantiateViewControllerWithIdentifier:#"CustomerDetailsView"];
detailsView.delegate = self;
detailsView.customer = _customer;
[scrollView addSubview:detailsView.view];
You could of course get the scrollView.contentSize from the child view controller you constructed in Interface Builder using scrollView.contentSize = detailsView.view.frame.size.
I have a love/hate relationship with Interface Builder... most days I love it, but some days we argue and I wish we'd never met... :)
only make cliptobound=YES in storyboard if you changed UIView to UIScrollView
Im trying to implement my own CustomUIActionSheet.
I have it almost working, but I have no idea how does the showInView method works.
(void)showInView:(UIView *)view
giving a view, this method is capable of put its view in front of every single view (adding it to the windows maybe?) but its also capable of settings the rotation accordingly to the view in which is being added.
Ive tried adding it to the windows of the view that I recieve as a parameter.
CGFloat startPosition = view.window.bounds.origin.y + view.window.bounds.size.height;
self.frame = CGRectMake(0, startPosition, view.window.bounds.size.width, [self calculateSheetHeight]);
[view.window addSubview:self];
self.blackOutView = [self buildBlackOutViewWithFrame:view.window.bounds];
[view.window insertSubview:self.blackOutView belowSubview:self];
By doing this, all works, except that when I present the action sheet in landscape, the action sheet apears from the right (since the windows system reference its always the same)
I´ve also tried to add it to the rootViewController of the view windows like that:
UIView * view = view.window.rootViewController.view;
CGFloat startPosition = view.bounds.origin.y + view.bounds.size.height;
self.frame = CGRectMake(0, startPosition, view.bounds.size.width, [self calculateSheetHeight]);
[view addSubview:self];
self.blackOutView = [self buildBlackOutViewWithFrame:view.bounds];
[view insertSubview:self.blackOutView belowSubview:self];
but again, it fails in landscape, it doesnt add anything or at least, I can not see it
So, my question is, any clue of how can I add a view to the top of the hierarchy working in both orientations?
thanks a lot!
After reading some source code of custom alert views and other ui components that are dranw in the top of the hierarchy I found a working solution.
Adding the view in the first subview of the windows:
UIView * topView = [[view.window subviews] objectAtIndex:0];
So the final code is
UIView * topView = [[view.window subviews] objectAtIndex:0];
CGFloat startPosition = topView.bounds.origin.y + topView.bounds.size.height;
self.frame = CGRectMake(0, startPosition, topView.bounds.size.width, [self calculateSheetHeight]);
[topView addSubview:self];
self.blackOutView = [self buildBlackOutViewWithFrame:topView.bounds];
[topView insertSubview:self.blackOutView belowSubview:self];
I got this warning:
'UIView' may not respond to 'addSubview:withAnimation:'
The line of code which produce that warning is this:
[self.masterView addSubview:self.detailImage withAnimation:def];
And my relevant code is like this:
ExUIViewAnimationDefinition *def = [[ExUIViewAnimationDefinition alloc] init];
def.type = ExUIAnimationTypeTransition;
def.direction = ExUIAnimationDirectionMoveUp;
[self.masterView addSubview:self.detailImage withAnimation:def];
[def release];
I looked on the UIView documentation, i thought addSubview may be deprecated, but it still like this.
Does any one know how to solve this warning? Thanx in advance.
addSubview is a method UIView will respond to. addSubview:withAnimation: is not a method UIView will respond to.
If you want to add a subview with a fade or something like that, try this:
self.detailImage.alpha = 0.0;
[self.masterView addSubview:self.detailImage];
[UIView animateWithDuration:0.3 animations:^{
self.detailImage.alpha = 1.0;
}];
To add a subview to a parent, call the addSubview: method of the parent view. This method adds the subview to the end of the parent’s list of subviews.
To insert a subview in the middle of the parent’s list of subviews, call any of the insertSubview:... methods of the parent view. Inserting a subview in the middle of the list visually places that view behind any views that come later in the list.
[self.masterView addSubView:self.def];
[def release];
This helped me to animate subview by sliding out from down of border of parent view
-(void) addAnimatadView:(UIView *) animatedView toView:(UIView *)aView {
CGRect frame = animatedView.frame;
float origin = frame.origin.y;
frame.origin.y = aView.frame.size.height;
[animatedView setFrame:frame];
[aView addSubview:animatedView];
frame.origin.y = origin;
[UIView animateWithDuration:0.4 animations:^{[animatedView setFrame:frame];}];
}
Just change frame origins as you want to reach free sliding