Make subview of a UIScrollView fixed while the other subviews scrollable - objective-c

I want to make a View with three subviews stacked on top of each other with the middle subview scrollable with the others fixed.
How can I achieve this programmatically? I have tried
to set the contentsize of the root view to the size of the scrollable view but that makes all the views scroll.
-set the contentsize of the middle subview without setting any property for the root view but that makes all the views unscrollable.
Please help. I am new to iOS.
Thanks in advance

You can use the scrollViewDidScroll: delegate callback on the UIScrollView to adjust your view's position. In the callback, get the contentOffset of the scrollview and use that to set your fixed view's position.
For example, if you want your fixed view to always remain 100 px from the top of the scrollview, set its initial frame to (0, 100, width, height), and then in the callback set the frame to (0, contentOffset.y + 100, width, height).
The result is that the subview will appear fixed at a given height.

If your UIScrollView has a superview (i.e. a container view), you can add your 'fixed' view as a subview of the superview instead of the UIScrollView. You'll only have to calculate your frame coordinates once.

You can do it moving sub view from UIScrollView to super view of scrollview like:
Place/set your button over scroll view (not inside scroll view) as shown here in this snapshot. And also set button constraints (position) with respect to super view of your scrollview.
Here is ref. snapshot of hierarchy of position of each view over each-other.

Related

How to implement ScrollView in cocoa app?

I'm trying to add ScrollView to my app. But when I place it to the window and add some objects inside, it doesn't scroll! What should I do to make it work?
Link to image: https://i.stack.imgur.com/ZcLCn.png
-> three buttons inside the ScrollView, while one of them is outside of the window's bounds
You should set the height of the contentSize of the scrollview larger than the height of the scrollview so that the scrollview can scroll vertically.To let the button outside the window be shown,the height of the contentSize should be larger than the maxY of the button

Scrollview vertical only with fixed width in Autolayout

In swift how can I set the width of the scroll view to be the width of the screen size?
I tried setting right leading and trailing bounds, but my aspectscalefill image keeps stretching the entire frame.
I am trying to force the view to be the width of the screen and allow vertical scrolling only.
If you want to make a scroll view with auto layout try this. No code at all so you will have to drag things out of the library to the right of Xcode.
In your view controller drag and place the scroll view size it to whatever you want but it looks like you want to make it the size of the screen. Pin all edges to the edges of the view controller. Pin trailing, leading, top, and bottom.
Now, instead of placing your items in the scroll view, place another view in the scroll view. With this new view you will place all of your items. You will most likely have to move the view up or down to place them all and resize the view. You can then place whatever constraints you want on your items. When all items are in their place, set the frame of the new view back to x = 0 and y = 0.
You will then place constraints as follows. Select the new view and pin to top, bottom, trailing, and leading and then center in container. This will make a constraint that is vertical with some negative number. In the storyboard outline select this constraint and set it to zero.
You will now be able to scroll vertically. Let me know if you have any questions.
I created a single view application, and added a UIScrollView in the storyboard with four constraints (top, left, right, bottom). And I add the following code in the viewDidLoad(). Everything works just fine. My image is only scrolling vertically, but not horizontally. No additional settings are needed.
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Just as an example, I make a size with same with of the view
// and twice the height of the view.
let size = CGSizeMake(
self.view.frame.size.width,
self.view.frame.size.height * 2);
// Load an image and make a image view
let image = UIImage(named: "im.jpg")!
let imageView = UIImageView(image: image)
imageView.frame = CGRect(
origin: CGPoint(x: 0, y: 0),
size: size);
imageView.contentMode = UIViewContentMode.ScaleToFill;
// Add the image view to scroll view
scrollView.addSubview(imageView)
scrollView.contentSize = size;
}
Your problem i caused by the fact that you add your elements to the scrollView from the storyBoard.
In the storyBoard you need:
1) Add the scrollView, if you need to be in the width of the screen then set it's trailing and leading spaces to superView to 0
2) uncheck the "allow horizontal scrolling" in the attribute inspector
In your code
1) set the scrollView's contentSize to what ever size you want it to be.
1.1) in your code you need to set the size of the contentView like so
scroller.contentSize = CGSizeMake(your width, your height);
This is a very important step, the scrollView will not be able to scroll if you don't set the size. Think about the scrollView as the window and the contentView as the view. If the contentView if the same size as the scrollView then the scrollView can't scroll anywhere (all of the contentView fits into the scrollView) in order to create the scrolling you need to make the contentView bigger then the scrollView itself
2) start adding your elements to the scrollView's contentView by calling
[scroller addSubView:<your view>];
this will add your views to the scrollView's contentView and will be

Necessary to remove ScrollView from Superview before changing and re-adding?

I have a UIScrollView that of course contains information. Based on conditions I make changes to the height of the scrollview as such:
CGRect scrollFrame = self.scrollView.frame;
scrollFrame.size.height = scrollFrame.size.height + adMobBannerView.frame.size.height;
self.scrollView.frame = scrollFrame;
I then add the scrollview back:
[self.view addSubview:self.scrollView];
All of this works as it should. However, should I first be removing the scrollview from the superview before adding it again? While again what I am doing works, I am wondering if I am just layering scrollviews on top of scrollviews unnecessarily?
You don't have to add UIScrollView as subview after you change its height (if it is currently added as subview).
When you try to add view A as a subview of view B and view A has superview it would be removed from its superview so you don't have to call removeFromSuperview method yourself.
From Apple Documentation:
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

NSScrollView content and scroller insets

What would be the best way to create content and scroller insets on an NSScrollView like one can set on a UIScrollView?
I'd like to be able to add a footer which is not spanned by the vertical scroller like that found at the bottom of Mail.app's sidebar
Have an NSView that contains both your NSScrollView and your footer's view, with the scroll view's frame set to not overlap the footer.

Drawing on UIScrollView and UIImageView

I want to draw line on image view then image view is to put on scroll view. How to do that? Any Idea???
Set the scroll view's contents to a UIView. Display the image in the UIView, do a setNeedsDisplay on that view, and draw anything you want when the UIView's drawRect gets called.