I have a view controller with a button. When the button is pressed, I want to add a subview which contains a scrollview. Right now my code displays the subview and scrolls fine as long as I don't add the xib. When I add the xib to the scrollview, the xib appears in the subview but there is no longer any scrolling.
The main View Controller:
-(IBAction)startNewSearch:(id)sender {
UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 600)];
UIView *myView = [[[NSBundle mainBundle] loadNibNamed:#"NewSearch" owner:self options:nil] objectAtIndex:0];
[myScrollView setScrollEnabled:YES];
[myScrollView setContentSize:CGSizeMake(320, 600)];
[myScrollView setShowsVerticalScrollIndicator:YES];
[myScrollView setShowsHorizontalScrollIndicator:YES];
[myScrollView setPagingEnabled:NO];
[myScrollView addSubview:myView];
[self.view addSubview:myScrollView];
}
The NewSearch class is a UIView. It's xib is 320 wide by 600 high.
I'm not sure if I understood your question exactly, but it may or may not be similar to something I did.
See if adding this will work:
-(void)viewWillAppear:(BOOL)animated {
[self.myScrollView setFrame:CGRectMake(0, 0, 320, 600)];
}
Sorry if this isn't what you're looking for... I too am having issues with scrollview stuff. Good luck!
This happens because you have same content size height as a frame height.
Try to use same frame size as your main view on your screen and content size of the scroll view should be bigger then height (in your case) or width (this depends on which scroll you need horizontally or vertically).
For example
CGRect scrollFrame = CGRectMake(0, 0, [self.view bounds].size.height, self.view bounds].size.width);
UIScrollView *myScrollView = [[UIScrollView alloc]initWithFrame: scrollFrame];
...
[myScrollView setContentSize:CGSizeMake(320, **800**)];
As you see there is height of content size bigger then frame height of the your scroll view. So it can scroll horizontally.
Also see the UIScrollView documentation.
I encountered a similar problem when I started using ios8. It turns out that touches were being trapped by something in the chain of view controllers. When I started using presentView Controller for the view controller containing the scrollview, then touches (and scrolling)started working again.
Related
I've just started working with XCode 5.1 and iOS 7.1.
Im having some problems with PartialViews containing a scrollview. I have this ). I marked in red the space the scrollview should occupy but its taking more space vertically and horizontally. The viewcontroller and view are defined in the storyboard as freeform of 500x500 and the scrollview is defined like:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, rowHeight*[lstMatches count])];
scroll.contentSize = CGSizeMake(self.view.frame.size.width, rowHeight*[lstMatches count]);
//.....more elements here, added as subviews of scrollview
[self.view addSubview:scroll];
The problem is the next:
1) The scrollview is wider than its container so I can't click over right buttons. I tried changing the width of the viewcontroller and view to 800 (max width is about 750), but i cant click them.
Thanks for your help
try:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height])];
scroll.contentSize = CGSizeMake(5000, rowHeight*[lstMatches count]);
[self.view addSubview:scroll];
PS: Change the contentSize.width of 5000 to something that suits your needs
I have the following ViewController:
It contains two UILabels at top, an UIImageView, below it a UITextView and below this a UIButton. I have arranged them using the Interface Builder following the blue line. All of this controls are inside a UIScrollView:
[self.scrollView setContentSize:CGSizeMake(320, 660)];
[self.scrollView addSubview:self.descriptionText];
[self.scrollView addSubview:self.descriptionImage];
[self.scrollView addSubview:self.titleLabel];
[self.scrollView addSubview:self.feedNameLabel];
[self.scrollView addSubview:self.load];
So when enabling Autolayout option, I just selected the ViewControler and then "Reset to Suggested Constraints in Description View Controller". But when I run the app, the scroll still appears for the entire page, but the only control scrolling is the UIButton. When scrolling up it will scroll below the UITextView.
I have made the UITextView to resize depending on the text, so I want my UIButton to always have the same distance to the UITextView. For that I have also set Vertical Spacing to the UIButton, but like this I don't have any scroll to my page.
Using the Autolayout for the first time, can I get some suggestions on what am I doing wrong ?
edit:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,660)];
[contentView addSubview:self.descriptionText];
[contentView addSubview:self.descriptionImage];
[contentView addSubview:self.titleLabel];
[contentView addSubview:self.feedNameLabel];
[contentView addSubview:self.load];
contentView.translatesAutoresizingMaskIntoConstraints = YES;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 660)];
//[scrollView setContentSize:CGSizeMake(320, 660)];
[scrollView addSubview:contentView];
[self.view addSubview:scrollView];
// to resize UITextField
[self.descriptionText sizeToFit];
[self.descriptionText layoutIfNeeded];
self.descriptionText.scrollEnabled = NO;
}
Autolayout is a bit tricky when it comes to UIScrollView subviews. I would recommend:
1-Embed all your controls (descriptionText + descriptionImage + titleLabel + feedNameLabel + load) into a UIView first, say contentView:
UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,660)];
//add all controls as subviews to contenView
2-Add contentView as subview of self.scrollView.
3-Keep the translatesAutoresizingMaskIntoConstraints property of contentView to YES.
I recommend you read this technical note from Apple here.
If you are using AutoLayout you don't have to set the content size of your scroll view. In fact, I believe it has no effect at all. It is set automatically from the constraints you are setting up. The trick is that you have to have at least one constraint related to every side of the scroll view, otherwise the content size will be wrong and it won't scroll. So for example, if you would have a really large image in it you would need 4 constraints connecting the sides of the UIImageView to the sides of the UIScrollView. You can read about this more here.
Using Storyboards, I have a UITabBarController with a UIScrollView inside, with a UIView inside the scrollView. My view is very large, and does not completely fit in the storyboard. I am not able to scroll all the way to the bottom of the View.
If you was building all of this from scratch, how would you go about it?
Say you have a UITabBarController with 2 views. hence you have two View Controllers. Lets call them 'A' and 'B'. Configuring two views within a tab bar controller shouldn't be too difficult if you refer to this link. You can achieve this using Storyboard too.
Now say, in view 'A' you want your large UIView within the UIScrollView. What you do is declare a UIScrollView with a framesize that is equal to the screen's dimensions but set the contentSize to your large size that you wanted. You can now have you large view within this scroll view.
Example:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 520)]; // little less than 568 to accommodate the tabBar
[scrollView setContentSize:CGSizeMake(320, 1500)];
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 320, 100)];
[scrollView addSubview:largeView];
[self.view addSubview:scrollView];
You can do the above in Storyboard as well but i prefer using code for such tasks.
Hope that helped. Let me know if you need more help.
Did you set contentSize for your scroll view? You should to set contentSize for scroll view to size of content view. For example if you want to show view in scrollView you should do something like this:
[scrollView addSubview:view]
scrollView.contentSize == view.bounds.size
I was able to figure it out. What I ended up doing was creating a new Storyboard with a tabView already implemented. Added a new scrollView to the storyboard tabView then copied my original UIView, that I wanted to scroll in, into the scrollView.
I draged the new UIScrollView out into my .h file and created a new property.
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
Then I set the new property up like so and viola!
[super viewDidLoad];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(320, 800)];
I have a simple viewcontroller with labels and text boxes. but to many to fit on one screen, so i would like to add a scrollbar. Do i need a scrollview for that? I only found tutorials about zooming with scrollviews. How to do it?
Yes, you should use UIScrollView. This is good example how to work with scrollView from Apple developer library
And documentation about UIScrollView
you need to add a scrollview and set its content size more than the actual size of scrollview. setting content size more than scrollview size will enable scrolling and will show scrollbars...
//Scroll Vertically
UIView *containerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 800)];
[containerview addSubview:your label];
[containerview addSubview:your textbox];
............
UIScrollView *scrv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrv.contentSize = CGSizeMake(containerview.frame.size.width ,containerview.frame.size.height);
[scrv addSubview:containerview];
[self.view addSubview:scrv];
I was wondering what was the real meaning of using initWithFrame with this scrollView, because we also set the dimensions of the scrollView after that, and we add the scrollView as a subView of the view.
So why do we need to specify this initWithFrame? I actually don't really understand it when the frame is self.view.frame (I would understand it better if we set a different rectangle, such as 0,0 50,50)
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(847, 1129);
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
Thanks
self.view in this case is the view containing the scrollview, so the scrollview fills the entire view when set to self.view.frame. Frame and content size are different things - frame of scrollview defines visible part of scrollview, and content size defines the size of scrollable (zoomable) content inside your scrollview.