how to expand the view width after loading ios?
I have a view that will be initialized with self.view.bounds
I want this view to have another frame after loading how can I do that?
FYI this view is a sub view of Scrollview.
So I am initializing a view, that is subview of a scroll view, inside a view.
If you are using storyboard and autolayout,
Update constants of width constrain of the view. You can create directly IBOutlet of the constraint and
int desiredWidth = 150;
_modifyViewWidthConstraint.constant = desiredWidth;
If the views are created programatically, then simply change the frame
modifyView.frame = CGRectMake(modifyView.frame.origin.x, modifyView.frame.origin.y,desiredWidth,modifyView.frame.size.height);
Keeping all its x,y and height same as previous.
If you are using Auto Layout on view and given width constant to view then change property of that constant,change relation from == to >= and change priority from 1000 to 250.Hope it will work you.
Related
I'm developing a little app in Swift 2.0. I have a View with the following hierarchy:
Now, the elements placed in this view can't be displayed entirely in it, so I would like to use a ScrollView in order to be able to scroll all the content.
How I can embed all the content of my Viewto a new ScrollView? Can I do this programmatically by code?
UPDATE: There's an easier way to do this which I didn't know when I posted this answer
1) Go to the viewcontroller on the storyboard and click on something in it and press Command + A. This will select all the elements in the view.
2) Go to Editor -> Embed In -> Scroll View. This will embed all the labels, views, etc into a scrollView.
3) Select the scrollview that you just embedded. Press Control and drag it to the respective class file of the viewcontroller and create an outlet scrollView.
4) Add this to your viewDidLoad()
scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)
Make sure the height for the contentSize is more than the size of your view.
ADDING SCROLLVIEW MANUALLY
1) Drag and drop a scrollview from the Object Library onto your viewcontroller in the storyboard and create an outlet to your program as 'scrollView'.
2) Click on your viewcontroller and go to the size inspector and note down the width and height.
3) Click on your scrollview and set the width and height the same as the viewcontroller. and set the X and Y values to 0
4) Click on the scrollView and drag it a little bit to the side
5) Press Command+A to select all the elements including scrollView. Press Command and click on the scrollView to deselect the ScrollView
6)You will have all the elements except the scrollView selected now. Now click and drag them into the scrollView.
7) Now click on the scrollView and set the X and Y values to 0 from the Size Inspector.
8) Add this to your viewDidLoad()
scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)
Make sure the height for the contentSize is more than the size of your view.
That should create a scrollView for your view. Some of the elements might be in a slightly different position. You can easily fix them by moving them on your storyBoard.
It can be done even simpeler than ebby94's answer.
1) Go to the viewcontroller on the storyboard and click on something in it and press Command + A. This will select all the elements in the view.
2) Go to Editor -> Embed In -> Scroll View. This will embed all the labels, views, etc into a scrollView.
3) Set the constraints of the Scroll View to the View's edges.
And you're good to go! No need for an outlet.
If you are using Snapkit or creating programmatically.
class ScrollViewController: UIViewController {
lazy var contentViewSize = CGSize(width: self.view.frame.width, height: self.view.frame.height + 320) //Step One
lazy var scrollView : UIScrollView = {
let view = UIScrollView(frame : .zero)
view.frame = self.view.bounds
view.contentInsetAdjustmentBehavior = .never
view.contentSize = contentViewSize
view.backgroundColor = .white
return view
}()
lazy var containerView : UIView = {
let view = UIView()
view.frame.size = contentViewSize
view.backgroundColor = .white
return view
}()
override func viewDidLoad() {
self.view.addSubview(scrollView)
self.scrollView.addSubview(containerView)
//Now Set Add your Constraints in the container View.
}
}
Above accepted answer explanation is enough to achieve the scroll view but I would prefer to create my complete application programatically and I don't use storyboards in my project. This code is for the folks who don't prefer to use storyboards.
Explanation
Step One: Determine your content Size. Here I am taking Exact width and adding 320 more to the height of the screen.
Step Two: Create a scroll view and add desire behaviour of the scroll view. Now, the contentSize of the scroll view should be same as the contentSize you've created above at step one.
By Following Step one and Step Two. You Will be able to set a scroll view on the top of the view. But If you want to add a stretching behaviour then You should follow Step Three
Step Three: Create a container view of the same size of the contentView which you've calculated in step one and set it to the frame of the containerView. By doing this you'll be able to achieve stretching header and footer behaviour in your screen.
Please read make sure to add constraints in the same order as it is set.
Answer Edits are welcome.
I had the same issue. I needed to add scrollview to the existing view.But my main container view has a lots of view inside it. And they were connected to each other. So i was afraid. Finally i did it. the process given below.
Duplicate your View (root View). For this first select the root view then press Command + D
Now delete all the child view view inside the root view
Now add a scroll view to the root view and set constraint to 0,0,0,0
Now add the duplicate(That you duplicated) view to the scroll view and set constraints to 0,0,0,,0 also set the height that you want.
Set width of the duplicate view by equal width with the root view.
Now select the viewController, go the size inspector, select free form size. Then set the height that you entered with duplicate view.
You have almost done. Now you have to connect the child view with outlet or action that you gave in the viewController class.
Thats all.
Selecting all elements and embedding scroll view (editor->embed in->scrollView) works fine.
Adding constraint is much more easy by selecting the constraint warning (Add Missing Constraints).
It's simple. Command A and Command X to specific view controller in StoryBoard. After that take scroll view. On scroll view, just take one view with view controller view height and width equal to scroll View width. Again do Command V and rearrange the constraints. Your problem will be solved.
After so much trouble i am able to scroll the scrollview, and it is working fine. Now my question is how to set the dynamic height of scrollview and main view after adding view inside scroll view. As adding height from IB make it static and inside content height may very.
steps that i followed are.
Added a uiview controller inside my storyboard.
Then inside this uiview added one uiscrollview.
Set the height of the main view to 1000 and scrollview to 1000.
Selected the uiscrollview and reset it to suggested constraint.
In this uiscrollview i added many labels, uiviews, textviews, etc programatically.
Now the main isuue is this 1000 height that i set in step 3 is static and the content is dynamic, so how to set height dynamically. and also is it necessary to follow step 4, when i am doing it programatically.
If you know the new subview's height, you can change the frame size by
// Add your subview to scroll view
...
// Set new height
CGRect newScrollFrame;
newScrollFrame.origin = MyScrollView.frame.origin;
newScrollFrame.size = CGSizeMake(MyScrollView.frame.size.width, MyScrollView.frame.size.height + MyNewSubView.frame.size.height);
MyScrollView.frame = newScrollFrame;
I'm trying to make layout inside scrollview using this one tutorial link
And get the following result link
It will be appreciated for any advices or tutorial links. It needs only vertical scrolling
I am sure there must be other ways to do this but a quick fix is :
1.) Create a width constraint on ContentView in Storyborad.
2.) IBOutlet that widthContraint and set its value to the view frame width in viewDidLoad.
Suppose the name of the constraint outlet is contentViewWidthContraint.
contentViewWidthContraint.constant = self.view.bounds.size.width;
Another alternative to do so from Storyboard, is to fix the Contentview width to the view's width from the storyboard or to the Scrollview, if Scrollview already has a Equal width contraint with superview . Add the "Equal Width" contraint from Contentview to either self.view or to Scrollview (if scrollview, already has the width contraint)
Have you set up the "ContentView" width to match with the scroll view width? I had the same problem and I fixed with "Equal Widths".
"Equal Widths" will tell to your "ContentView" to use the same width of the "Scroll View", which should be fitting the screen if you have set up the constrain properly.
You can do this easily on the storyboard.
Drag and drop, with right click (important!!!), from "ContentView" to "ScrollView"
Release the click, you will be prompted with a menu, select "Equal Widths".
This should fix your problem using the scrollview with AutoLayout from Storyboard editor.
You can find a full tutorial how to use ScrollView with Autolayout and Storyboard here.
I hope this is useful for you :)
In the Storyboard set the width of the elements contained in your UIScrollView equal to the width of this UIScrollView (by selecting all elements and the UIScrollView holding in the panel on the left of your Storyboard and then setting the 'Equal Widths' constraint under 'Pin' on the bottom of your Storyboard). Just pinning the right sides of the elements to that of the UIScrollView won't work as it will adjust the size of its "display view" to the width of the largest element and if this is smaller than the width of the UIScrollView all elements will just appear aligned to its left side.
There is also another possibility that offers a very good result.
You can mark a checkbox:
O programmatically:
scrollView.alwaysBounceVertical = true
Try to set it's width to 0 & height equal to content size like this:
self.scrollView.contentSize = CGSizeMake(0, self.scrollView.contentSize.height);
This will work as you want. Try it & tell if still facing any issue.
For disabling the horizontal scroll, you can set the content size in the -(void)scrollViewDidScroll method.
[self.scrollView setContentOffset: CGPointMake(0, self.scrollView.contentOffset.y)];
self.scrollView.directionalLockEnabled = YES;
This is because scroll view have no idea where your content should end.
But when at least one item inside your scroll view has its "trailing space" constraint attached to a view outside the scroll view (usually a view the scroll view is sitting in or some other view of a higher level, which "knows" its width) - the scroll view will automatically get an idea about your wanted width and won't scroll horizontally (unless that trailing constraint implies having your content outside the screen).
Better if all items inside scroll view have their "trailing space" constraints connected either to each other or to a view outside the scroll view. But not the scroll view itself.
No additional code or extra constraints needed for this to work.
Too set UIScrollView constraints as like below code so it will occupied whole screen.Not exceed the screen size.
Leading Space = 0 from mainView
Top Space = 0 from mainView
Bottom Space = 0 from mainView
Trailing Space = 0 from mainView
You need to set the width of UIScrollView equal to or less than the width of your Parent View. Two ways to do it:
1) You can do this in Storyboard via layout constraints
2) You can do this programatically:
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.contentSize.height);
I need to display the layout I have in the picture in my app.
The view hierarchy is:
scroll view (scrollEnabled = true)
image view
label
webview (scrollEnabled = false)
table view (scrollEnabled = false, max amount of rows = 6)
The web view height is dynamically changed by it's delegate webViewDidFinishLoad.
func webViewDidFinishLoad(webView: UIWebView) {
webView.sizeToFit()
}
How can I set up the auto layout to move the table view below the web view?
First of all you need to add containerView in scrollView and then add subViews to containerView.
Below I’ve displayed the hierarchy you need to follow for displaying content in UIScrollView and the constraints you need to set.
scroll View (constraints :- leading,trailing,top,bottom)
Container View (constraints :- leading,trailing,top,bottom,widthsEqually to View,heightsEqually to View or height = 1000)
NOTE : If all subViews has specific height then there is no need to give height to ContainerView.
UIImageView (constraints :- leading,trailing,top,verticalSpacing,height)
UILabel (constraints :- leading,trailing,top,verticalSpacing,height)
UIWebView (constraints :- leading,trailing,top,verticalSpacing,height>=0)
UITableView (constraints :- leading,trailing,top,Bottom)
NOTE: give height to tableView if required. You will need to make changes in height constraint of UIWebView programmatically at run time based on your requirement.
For more reference please refer links below:
http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/
http://blog.surecase.eu/working-with-uiscrollview-in-storyboard-using-autolayout/
http://makeapppie.com/2014/12/11/swift-swift-using-uiscrollview-with-autolayout/
I have a view that is larger than screen, I need to put it in a UIScrollView.
So I first add an UIViewController to story board, then, I add a UIScrollView to the root view of my view controller, then when I add subviews of UIScrollView, but I can't add them outside the scrollview area I can see, how to solve this problem?
Change the simulated size of the view controller to Freeform and then you can set the size to whatever you want. After editing, you may want to set it back.
Edit 1:
1) put your subview into scrollview's visible area
2) change frame of the new subview as you wish
3) change contentSize property of scrollview with runtime attributes
Edit 2:
u can create a new view with xib, which contains all your subviews, and then add this view on scrollview OR u can use storyboard's Container View like this:
p.s.: don't forget about contentSize (point 3 in my first edit), but if you are using auto-layout, you need to set it programmatically like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentSize = CGSizeMake(320, 250);
}
put a scroll view in the view controller ,and put a view inside the scroll view named containerView.
design the big content in another view controller (named bigContentView)
in code ,view load event ,
a. get the bigContentView by storyboard Id. ( get the controller and use controller.view)
b. create the outlet to the containerView.
c. update the containerView's frame according your requirement.(update the width and height)
d. bigContentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
e. containerView addSubView:bigContentView.