Lock UIView position relatively to UIScrollView - ios

I am trying to create an interface that shows a floor plan with markers in fixed positions so when user zooms or scrolls the floor plan, markers stay on the same position relative to floor plan image.
So far I tried to do it with a UIScrollView that holds an UIImageView and then I tried to add some dummy subview
let view = UIView(frame: CGRect(origin: CGPoint(x: 400, y: 200), size: CGSize(width: 36, height: 36)))
view.backgroundColor = UIColor.redColor()
scrollView.addSubview(view)
but origin (in my example 400,200) is relative to screen, which means that the dummy subview shows only when you zoom enough and then slides along if you scroll around.
I can't figure out how to fix this. Maybe I should use some other UIKit classes?

Try that
create a container view of the preferred size
add the image view as a subview of this container view
add you dummy subview in the container view
from the zoom delegate method of the school view return the container view
The fact is that they need a common ancestor view

Related

UIView detaches from bottom of UICollectionView on scroll

I have a UICollectionView that allows a user to enter reps via a UIView containing a picker. The picker container attaches to the bottom of the view as long as the UICollectionView doesn't need to scroll. As more cells are added and the view must scroll, my picker container no longer attaches to the bottom of my view. I have attached a screenshot of my issue and code showing how I have attached my pickerContainer to the bottom of my collectionView.
let pickerContainer = UIView(frame: CGRect(x: 0, y: self.collectionView.frame.height - pickerContainerHeight, width: self.collectionView.frame.width, height: pickerContainerHeight))
<....some other code that i dont think matters....>
collectionView.addSubview(pickerContainer)
Don’t add it to the collection view as it will be added to scrollable area.
You can add it to the superview of collection view and put it above collection if you using storyboard. Or move in front using pickerContainer.superview?.bringSubviewToFront(view: pickerContainer) in case you interested in only in programmatically version.

Create onboard screen in swift

I have created onboard viewcontroller using UICollectionView
but when I swipe, the picture goes out of the iphone, I want the picture to clip inside the phone and show other image. How can i achieve that?
Make the frame of the collection view fit the the screen of the phone image.
For example if the upper left corner of the screen of your phone image is located at x:20, y: 40 and the screen of your phone image is 160x300 big, then the following should be your collection views frame:
collectionView.frame = CGRect(x: 20, y: 40, width: 160, height: 300)
If you are using AutoLayout, make sure that the edges of your collection view are lined up with the edges of your phone images screen.

how to drag UIScrollView to the very right depending on its childs

This is a very simple question,
I have a UIScrollView where I am setting childView horizontally from left to right. each time I am adding a new view, I want my ScrollView to scroll to the right so that the newly added view is seen on the screen.
When you add a new view you need to first increase the scroll view's content size.
scrollView.contentSize = CGSize(width: childV1.frame.width + childV2.frame.width, height: scrollView.contentSize.height)
Then you should be able to call scrollRectToVisible and pass in the most recently added child view's frame.

UIPresentationController - presentation view size?

I'm using a UIPresentationController, but I can't sort out how to get it showing the way I want it to.
I'm editing the frameOfPresentedViewInContainerView() function, and I need to be returning the frame (a CGRect) to display my content in.
The tutorial I followed uses CGRectInsert(self.containerView.bounds, 50, 50), which makes the window centered with the borders brought in 50px. If I return self.containerView.bounds just as itself, the view takes up the whole screen.
I'd like the overlaying view to be the width of the parent view (so, self.containerView.bounds.width), but I want the height to be the size needed to show the content of the new view without cutting anything off.
I tried a CGRect at (0,0) with width and height from self.preferredContentSize, but it's not returning sizes that work.. What can I do?
I tried frame = CGRect(x:0, y: self.containerView.bounds.height/2, width: self.containerView.bounds.width, height: self.containerView.bounds.height/2) just as a test (but that's just making the view half the size of the parent view), but when I rotate the screen, suddenly the new view is almost off screen..
Aside from frameOfPresentedViewInContainerView(), I also had to add the following to make it work.
- (void)containerViewWillLayoutSubviews
{
self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}

UIView appears with wrong center position

In the following function I define all the necessary characteristics of a UIView:
func OpenController() {
var gameOverView: UIView = UIView()
gameOverView.center = super.view.center
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.backgroundColor = UIColor.grayColor()
self.view.addSubview(gameOverView)
}
Even though I define the center of the UIView "gameOverView" as that of the the viewcontroller it resides in, it appears with a corner in the center of the viewcontroller and not centered in the viewcontroller. I have tried various other ways of defining the position (NSLayoutConstraints, frame.x, frame.y etc.) but all have this result.
If anyone can tell me why this happens and how to center the UIView within its parent view controller I would greatly appreciate it!
Your issue here is that your center is being set before the frame. Since you are creating the view without the frame argument your frame is {0, 0}.
So you are currently centering the subview then resizing it, so this is happening:
What you need to do is resize the subview then center it, like this:
So you can just swap your centering and framing logic:
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.center = super.view.center
Otherwise even easier just pass the frame when creating the view (you could even pass in the proper x, y coordinates to center it here too):
var gameOverView: UIView = UIView(frame: CGRectMake(0, 0, 200, 300))
You have to set the center of the view after setting its size. This is because before setting the size or frame of a view its frame rect is (x:0,y:0,width:0,height:0). If you then immediately set its center, then a view of size zero will get centered so its new frame rect could be (x:20,y:20,width:0,height:0) (if the parent view is 40x40). If you now change the view's size, the origin point of the view will not actually move so the new frame could be (x:20,y:20,with:5,height:5) which means the view is no longer centered.
So to center a view you have to first set its size and the its center.

Resources