Relative position unreliable on embedded view - ios

I have a viewcontroller. On my storyboard I have dragged a TextField onto it.
In my code, I define a rect, that needs to be positioned relative to the text field, as follows:
CGRect autocompleteRect = CGRectMake(schoolField.frame.origin.x, schoolField.frame.origin.y + 70, schoolField.frame.size.width,autocompleteTableView.contentSize.height);
That works fine, until I embed my TextField in a UIView.
The UIView itself is managed by auto layout to be positioned in the center of the Controllers .view.
I would say, that this should not be of any influence to the relative postion of my rect. However, it is:
Since the embedding of the TextField in a UIView, my rect is positioned differently, also depending on the screen size of the device I'm running it on.
So, I guess it's got something to do with AutoLayout,but I wouldn't know what I should tell my app to behave the way I want it to.
Thanks for your insights.

autocompleteRect is a rect inside CredentialsContainer, but you use it for a view which has been contained by UIViewController's view. That's why your autocompleteRect has wrong position when you use.
To solve issue, you need to add the view which will use autocompleteRect to CredentialsContainer.
Hope this can make you more clearly.

Related

How can I get the correct sizes for UIViews created in storyboard during runtime

I created a UIView using storyboard, set up constraints. When I ran the application, the interface looked fine. The view covered the entire screen as it was supposed to (I made the background color black so it's easier to see what's going on).
http://contentlibrary.sinaapp.com/View%20from%20Storyboard.png (I'm sorry I have to link images in like this, but the system says I need at least 10 rep to post images.)
However, when I tried to get the size of that view using myView.bounds.size. I got (600, 580), which is exactly the size that appeared in the storyboard's size inspector.
I created another UIView in code
var myView = UIView(frame: CGRect(x: 0, y: 20, width: 600, height: 580))
and added it as a subview to the top-level view. It appeared but it didn't cover the entire screen like the view I created in storyboard. Both the view I created in storyboard and in code have the same value in their .bounds.size, but they appeared to have very different sizes on screen. (This picture is the view created in code. See there is white space at the bottom of the screen.)
http://contentlibrary.sinaapp.com/View%20from%20Code.png
Another interesting thing is, if I replace the above UIView with a UIScrollView, after the scroll view zoomed, it will update to the correct size.
I added a UIScrollView the same way I added the UIView using storyboard. I tried to print its size inside the delegate method scrollViewDidEndZooming:withView:atScale:. It says (375.0, 647.0), which is the correct on-screen size for a view that covers the entire screen on an iPhone 6.
Judging from this, it seems that views that come out of a storyboard don't automatically update to the correct on-screen sizes.
So how do I get a UIView object created in a storyboard to update its size?
Sounds like you're checking the size too early.
Try to put your code in viewDidLayoutSubviews method and see if it's going to change.

How to zoom on UIView

In my iOS app I want my users to be able to zoom in on the screen. My main uiview contains several subviews which contain images. I want my uipinchgesturerecognizer to either change the scale, or ideally use some "zoom" rather than scaling each subview.
Please and thank you.
This can be accomplished with UIScrollView. First create a scroll view as the base of your view hierarchy, putting your previous container view as a subview of the scroll view. Set the delegate of the scroll view to self and implement the delegate method viewForZoomingInScrollView, in which you should return the view that will be zoomed in (your original container view). This will allow the user to pinch and zoom your original UIView.
It's hard to provide advice on this without having a clearer view of what exactly you want to achieve.
Can you include a link to a sketch? For example, do you want the individual subviews to remain the same size but the layout to change ? Do you want the individual subviews to resize but their contents to be upscaled?
If you simple want to treat the subview as (basically) a single image which just happens to have other images in it, then maybe it would be better to render it as one and then scale that?

ios: Get size of parent popover from child viewController?

I have a viewController which is presented inside a popover. I'm trying to figure out how to size the viewController's view (viewController's self.view) to match the popover.
I would think the view would automatically be size to fit into the popover; for some reason, it isn't. The view's frame is the size of the entire screen, for some reason.
How do I either: detect the size of the surrounding view controller so I can resize the view; or, cause the view to automatically size itself to the popover?
Note: This is a class which is embedded into someone else's app, so I have no control over the popover size itself.
Figured out what I was doing wrong: I was looking at the frame size in the viewDidLoad method. It's not always valid at that point, for some reason.
The correct place to check the frame size is viewDidAppear; it seems to be valid there. Admittedly, though, I haven't checked this in every single corner case, so YMMV.
Override contentSizeForViewInPopover for your view controller and return the size you need the popover to have. So, it somehow works the other way around, the contained view controller lets the popover know how large should it be. The popover is 320px x 1100px by default I think. If you overwrite the method I mentioned above, you'll make it as large as you want it to be, even if the code is embedded in an app and you don't have control over it.

How can I make one part of view scrollable?

This has probably been asked a bunch, so I'm more looking for direction to somewhere I can research this, unless it is an easy straight-forward answer. I can't think of the propper terminology to search for it; I just keep getting the same standard UIScrollView info.
I understand how to make a scrollable view, but I can't find anything on making say a 310 x 310 pixel part in the center of my view to be scrollable, like a scrollable div in the center of a webpage so it can have a nice image border around the content that would scroll in the center.
Just create a UIScrollView and set its frame to the size and location that you want and then add it to another view.
You can accomplish this with the addSubview method of UIView.
Create a UIImageView that spans the size of the screen (or whatever space you would like it to take up). This will contain the 'image border' you refer to. Let's say that view is named backgroundImageView.
Then create your UIScrollView; let's say it's called myScrollView. Now for this line:
[backgroundImageView addSubview:myScrollView];
That will add myScrollView inside backgroundImageView in the view hierarchy. Now you set myScrollView's position in the coordinate system of backgroundImageView. So to center it, do this:
[myScrollView setFrame:CGRectMake((backgroundImageView.frame.size.width - myScrollView.frame.size.width) / 2, (backgroundImageView.frame.size.height - myScrollView.frame.size.height) / 2, myScrollView.frame.size.width, myScrollView.frame.size.height)];
And voila, you're done :)

UIView coordinate transforms on rotation during keyboard appearance

iPad app; I'm trying to resize my view when the keyboard appears. It amounts to calling this code at appropriate times:
CGRect adjustedFrame = self.frame;
adjustedFrame.size.height -= keyboardFrame.size.height;
[self setFrame:adjustedFrame];
Using this technique for a view contained in a uisplitview-based app works in all 4 orientations, but I've since discovered that a vanilla uiview-based app does not work.
What happens is that apparently the uisplitview is smart enough to convert the coordinates of its subviews (their frame) such that the origin is in the "viewer's top left" regardless of the orientation. However, a uiview is not able to correctly report these coordinates. Though the origin is reported as (0,0) in all orientations, the view's effective origin is always as if the ipad were upright.
What is weird about this is that the view correctly rotates and draws, but it always originates in the literal device top left. How can I get the view to correctly make its origin the "top left" to the viewer, not the device's fixed top left? What am I missing? Please, for something so trivial I've spent about 6 hours on this already with every brute force technique and research angle I could think of.
This is the original source which doesn't work in this case:
move up UIToolbar
OK, I don't know what the ACTUAL answer is to the original question, but I can say with certainty that one way to resolve the issue is to always ensure that you don't manipulate a viewController's view directly. Always wrap your view inside a container view inside the main "view", then have that container view adjust its position etc as needed. Works exactly as the splitview does, probably because in both cases now the view in question is a subview of the main "view". What a relief!

Resources