I have a UITextView Object. I want to add another view under but the TextView fills the whole screen. Couldn't find how to do. What I want to do is show a fixed view when the scroll is finished. How can I do it?
Don't use the textViews own scrollView for scrolling.
Instead setup an outer scrollView that contains the textView and your footerView (either by embedding in a stackView or by setting up your own constraints).
Getting text and scrollView resizing right can be tricky.
Please refer to this guide for details.
To summarize:
Add UIScrollView inside the main view in Storyboard
Add UIView inside the UIScrollView
Add UITextView inside the UIView (the view added in step 2)
Make sure "Scrolling Enabled" of UITextView is unchecked
Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
Add UITextView height constraint IBOutlet on the ViewController. #property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; and connect it in Storyboard
Change the UITextView height constraint programmatically. self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
Related
I've added a scrollview to a view in my app, the view is selected from a tab controller and as such is a container and not a UIViewController it is declared as below
class HomeViewController: Container, UIScrollViewDelegate {..
I've added the scrollview in the storyboard and added all of the components to it and I've assigned the delegate from the storyboard and I've placed this code in my ViewController
scrollview.contentSize = CGSize(width:1048, height:scrollview.frame.height)
scrollview.delegate = self
This how my ScrollView looks in my storyboard, you can see that scrollview is my UIScrollView, then I've added a UIView viewScroll and then added four views to viewScroll (View1, View2, View3 and View4), scrollview is sized at 375x340 and viewScroll is set at 1048x340
When I run the app it does not scroll. As it is not a UIViewController, do I need to approach this differently or is there something else I have missed?
I've added the scrollview in the storyboard and set the width as 1050 and height as 330
You should set the width and height of scroll view to be at most as large as its containing view. What you need to set to 1050 and 330 is contentSize of the scroll view - scrollView is only scrollable when its contentSize is larger than its bounds.
If you're setting up your scroll view using Interface Builder, then you'll need to add constraints to its children's edges. For example, in your case you'll have to add leading, trailing, top, and bottom constraints to your View Scroll view. Select it and add following constraints in interface builder:
After that there will be something like this:
When you add edge constraints to UIScrollViews child views, you are hinting the scroll view about its content size. And if scroll view knows its content size and it is larger than scroll view's bounds, you'll have scroll working.
In my app there is a viewController placed inside a NavigationController and TabBarController. a viewController has 1 stackView inside.
I've dragged 3 items inside a stackView: UILabel, MKMapView and UILabel
MKMapView is in the middle.
I want my MKMapView to be about 0.8% of the StackView Height (so that it would stretch if the device screen is big and shorten it's height if it is small) I want my UILabels to have a fixed height of 30px.
How to implement this the right way using constraints in Main.Storyboard.
Which way is the most appropriate?
I've tried to do it the following way:
I've added UIStackView and inserted UILabel, MKMapView, UILabel inside. My stackview now has (alignment: fill and distribution fill set)
After i've added the following constraints to UIStackView
I've changed the following properties in UIViewController because if not set, the UIStackView is hidden by TabBar and NavigationBar (maybe this can be fixed, do not know how)
My storyboard looks the following way after setting 1-3
Now when I want to set label1 and label2 height to 30px and when i do that i receive "frame will be different at runtime" error
What am i doing wrong? How to do this nesting with setting the heights of nested elements right? I'm using Xcode 8 beta 5 with Swift 3.
I added a UIViewController, then in the XIB i dragged a UIScrollView, and a few buttons at the bottom of the page. Then when i build and ran the application, the application scrolls but the button that i added at the extreme bottom of the page can't be seen properly. So i think i will have to set a height for the scrollview. But where in Interfacebuilder i should specify the height ?
If it can't be done in Interfacebuilder, then can someone show me how to do it programatically ?
You need to set the contentSize property of the scrollview. You cannot do it in Interface Builder. You need to set it in the code of the view controller that manages the scroll view.
scrollView.contentSize = CGSizeMake(width, height);
I am working on an application where i need to place a uiscrollview within an uiview. The view has a fixed back ground, and in lower half of the view i have to place 30 buttons which scrolls within that area. Lets say the uiview is of 320x480, then the scrollview i have to place will be at (40, 160) to (280, 300). I just need the scroll view to have the 30 buttons inside it. so that view remains fixed, only the buttons can be selected by scrolling vertically. How can i do that?
When i place the scroll view and connect it with file's owner 'view' it eats up all the view space.
Yes, i am a newbie :$
First Add UIScrollView to the view:
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(40,160,280,300)];
scroll.showsHorizontalScrollIndicator = YES;
[self.view addSubview:scroll];
// Now Add your Buttons on this scrollview;
[scroll addSubview:buttonObject];]
If you wanted to add 30 buttons, you can use 'for' loops to allocate UIButton's and then add it on scrollview.
The UIViewController sub class (your files owner) will only show the one view you connect to it's view outlet. So you should connect the outer view to files owner, and then add the scroll view to the view by dragging it into the view, and sizing it as you like.
You probably want to create another outlet for the scrollview, so that you also have access to the scrollview from your controller. You could to this by adding the following to you controllers h file and then connecting the scroll view in interface builder.
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
I am building a data entry form in my iPhone app, and there are more data fields than will fit on the screen. I figured I should put them into a UIScrollView so that the user can scroll through the form. What's the best way to build this in Interface Builder? I know that I can do it programmatically, but I'd like to do it in Interface Builder if I can. The problem is, how can I lay out UILabels, UITextFields, etc. if they fall outside of the main iPhone screen--in the part of the screen for which the UIScrollView becomes useful?
The best workaround I found for this problem (which I consider embarrassing for Interface Builder) is this:
place a "container" UIView inside the UIScrollView, set the size of the container UIView to what is needed (e.g. 320 x 1200) with the inspector and add your content inside that container view. (your buttons, textfields, etc).
set the contentSize for the UIScrollView, in code, to be the same as the size of your container UIView. somewhere in viewDidLoad for example (e.g. scrollView.contentSize = containerView.frame.size;)
To modify content beyond the scrollview's bounds in Interface Builder, you have to drag your container view outside of the scroll view each time, make your modifications, then drag your container view back inside the UIScrollView and build.
This is actually straightforward:
Create a new view controller class e.g. MyScrollViewController
Create a new xib with a UIScrollView as the topmost view, and set the class of the File's Owner to MyScrollView Controller
Set the view attribute of File's Owner to the scroll view
Drag the bottom of the scrollview to create the desired size.
Position your various other UI elements as sub-views of the scroll view
Create and connect an IBOutlet in MyScrollViewController.h for the scroll view, e.g.
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
In MyScrollViewController's viewDidLoad method, add the following line of code:
self.scrollView.contentSize = self.scrollView.frame.size;
Th-th-th-that's all folks!
Set up "Content Insets" of "Scroll View Size" on the "Size inspector": Bottom = YourFrameHeight - ScreenHeight. It will allow you to scroll in ranges of top-to-top, bottom-to-bottom of your UIScrollView
Double click to open the UIScrollView itself in IB, and increase the size to the size you need or bigger (you can always shrink at runtime). Then just add the elements.
EDIT:
Doesn't work - see here instead.
I've had the same issue as well. What I ended up doing was putting another UIView inside of it and setting the height to whatever I wanted. Then I put all my UI elements inside of it. This allows you to drag the inner view up and down.
What worked for me in xCode5 (no storyboard, using autolayout) is using the 7 step answer above the ends with 'Th-th-th-that's all folks!' and adding two steps.
8) Drag a new UIView to interface builder. Not into the Scroll view. Just on its own. Put all your controls/view into that and make it as big as you want. I hooked up this view as contentView.
#property (weak, nonatomic) IBOutlet UIView *contentView;
9) Then in - (void)viewDidLoad
[self.mainScrollView addSubview:self.contentView];
[self.mainScrollView setContentSize:CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height)];
Just saying that made it work for me.
There is finally a sensible solution to all of this and that is to place a Container View inside of the UIScrollView. As Interface Builder displays the UIViewController inside the Container View separately, you can finally see what your UIScrollView content is going to look like without crossing your fingers and standing on your head.
You still need to programmatically set your content size but a least you can now visualise what it going on.