In an app I created, I drag and dropped a 'Scroll View' to the main storyboard. This automatically creates a NSScrollView, inside of which is an NSClipView, inside of which is an NSView. Fine.
What I would like to achieve is to dynamically add checkboxes to that scroll view. I don't know how many checkboxes there will be in advance (which is why I need a scroll).
adding a subView to either the NSScrollView or the NSClipView actually displays some checkboxes, but the scroll is not enabled. Adding a subView to the NSView renders nothing.
Edit: I have also tried to modify the NSScrollView, NSClipView, and NSView's height, with no luck.
How can I enable the scrolling when too many checkboxes are added to the view?
So the trick to this is creating a container view (i.e. NSView) to hold the buttons. When you create a NSScrollView in the storyboard, it creates NSClipView and NSView for you. After you add or remove a button to the NSView (you can create your own, or use the one provided), set the documentView of the scroller to the view that contains your buttons. That resets everything properly in the scroller, and you should be scrolling! If you change the size of the view that represents the scrolling content, you just reassign that view to scroller.documentView and it will update everything accordingly.
Here is a horrible example of adding 20 buttons to a scroller that will scroll:
let documentView = NSView(frame: CGRectMake(0,0,200,1200))
for index in 0..<20 {
let offset = CGFloat(index * 50)
let button = NSButton(frame: CGRectMake(0, offset, 150, 50 ))
documentView.addSubview(button)
}
scrollView.documentView = documentView
The Apple documentation on this gives more detail and further options you may want to set.
Hope this helps!
Related
When I add a custom NSView to my NSScrollView via documentView and resize the window the content sticks always to the bottom of the window (so the scroll view is always scrollt to the bottom when the window is resized).
Is there a way to keep the scroll view scrolled to the top when the window is resized?
Edit: Uploaded reproducible example:
https://github.com/nathasmike/sample1
The autoresizing mask of the custom view is translated into constraints. To prevent this do
myCustomView.translatesAutoresizingMaskIntoConstraints = false
myScrollView.documentView = myCustomView
Add constraints to the custom view and its subviews.
Another solution is removing the autoresizing mask from the custom view in the xib.
I'm currently making a drawing application in Swift, but I wanted the page to be able to expand if the user wanted more room. I figured that I would use a UIScrollView to scroll around the canvas, but I wanted it to expand whenever the user went to the edge of the page, and for the UIImageView that I am drawing on to expand with it. Does anyone know how I would go about doing this?
Thanks!
In storyboard, drag a UIView into your UIScrollView then constrain that view to all sides of the scroll view. Then set the width to whichever value you would like (I would recommend to control drag from your view to the root view of the view controller and select equal widths), then give it a height constraint. Next, you need to connect an IBOutlet to your view controller code for the height constraint you set on the content view inside the scroll view. When you need to extend the page, add to the value of the height constraint and call layoutIfNeeded() on the scroll view.
I'm midway of finishing my app. I noticed that one of my views needs extra vertical space so I need to add a UIScrollView. Yet when I add that scroll view it overlaps everything else in the view. In simple words if I need to get it to work properly I have to delete everything off the view , add the scroll view, and then re-add everything back! Is there anyway to send this scroll view to the background of the view? This is all the code that concerns the scroll view
#IBOutlet var scrollerForPOfFourBar: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
/* non related code here*/
scrollerForPOfFourBar.userInteractionEnabled = true
scrollerForPOfFourBar.self.contentSize = CGSizeMake(320, 400)
}
I'm assuming you're using Interface Builder since you marked your scrollView as an IBOutlet.
On the view list, on the left, the order of the views specifies their Z order, with the views at the bottom being the ones drawn on top (with the largest Z).
To move your scrollView to the back, drag it just underneath of the view, and drop it there.
Keep in mind that you will have to drag all the views you had in your view into the scrollView (the easiest is to do it in the views navigator as well), and you'll have to set up any Auto Layout constraints again, which will be a pain.
Are you just want to move scrollerForPOfFourBar to back? Try this:
self.view.sendSubviewToBack(scrollerForPOfFourBar)
I have a problem with my custom cells.
My cell is structured like this image:
Gray view = cell.contentView
Orange view = a button added as a subview to the contentView
Yellow view = a label added as subview to the contentView
Blue view = an image view added as subview to the contentView
Green view = accessory view
The problem is that when I toggle edit to tableView, my cell indents hiding accessory view and moving right everything else. I want to avoid this.
When I toggle editing mode, my cell mustn't move its content, the editing control (minus red button on the left) must take the place of the button inside the contentView and this last one should hide itself.
I tried tableView:shouldIndentWhileEditingRowAtIndexPath: method without success because my tableView is plain. I also tried to override willTransitionToState: method but I don't know how to do what I want.
Can someone help me?
Thank you so much!
How have you added the subviews to the cell? If you add them directly to the cell instead of adding them to the content view, they won't get moved. It is the content view that is resized when the cell transitions to editing mode.
If you want to hide certain subviews, you should override setEditing:animated: in the cell subclass and hide / show as appropriate.
You may also need to add your subviews such that they are below the contentView in the view hierarchy, or bring the content view to the top after you have finished, using the bringSubviewToFront: method.
If you really don’t want the cell to resize, you could implement -layoutSubviews thusly:
-(void)layoutSubviews
{
if ([self isEditing] == NO) {
// Lay out subviews normally.
}
}
When you go into edit mode, -layoutSubviews won’t follow the code path that lays out your views. This is assuming you’ve subclassed UITableViewCell.
HI FRIENDS..
I use a scroll view and a label now i want that when i scroll that , label also scroll with that and set to next location , i set paging enable = YES , so i want that there are 5 images and when i did scrolling the label i set is also move and show in other position.
thanks
To scroll a UILabel (or anything for that matter) WITH a UIScrollView, simply add the label to the UIScrollView.
You can easily do this in Interface Builder by the obvious methods.
You can also do this programmatically by utilizing the addSubview: method.
You can do one thing you change frame of label when you scroll in delegate method of scrollview or you can also make 5 same label in all 5 pages.
Good Luck
Well if you have added a scrollview on top of a uiview you can add that label on the view and then bring it to front. then you dont have to worry about changing frame of the uilabel as it will be always on top of the scrollview and at the same position and also you can change the text of that label when the user scrolls in pagecontrol value changed method.