Scroll view white space in the bottom ios - ios

Hello i am working on the scrollview followed many tutorials found on stack overflow and youtube but facing same problem in the all tutorials , i want that if there is no content in the bottom there should be no white space whatever the view size is, in this pic you can see there is no content in the bottom many so much white space there are two labels in this image one is on footer and second is in the center i want that if there is nothing after the center there should be no more scrollview how to rid of this please guide me
this is my storyboard

Firstly, The problem is that if you want to the scrollview to be just the size of the content you have then you need to have to set
scrollView.ContentSize = CGSize(width: view.frame.width, height: view.frame.height)
If the problem is not solved with this then you have to check your constraint that must be incorrect.
Also to get the correct height you must have added bottom constraint to last element i.e footer i.e footer.bottom = safeArea.bottom (in storyboard inspector)
Hope this solves your problem.

Related

set scroll view that contains views and stack view doesn't fit on simulator . iOS

Hi every one actually I want to fit this scroll view and stack views on simulator correctly but unfortunately this result not things that I design to view controller and here is my constraints :
if you have any idea , please share thank you so much.
here is when I result after the answer :
The problem is Stack View.height = height, StackView's height should be automatically driven by it's subviews.
After you remove this, you should see another issue that's caused by Stack View.centerY = centerY - you should be able to remove this as well and you should get the expected behavior with rest of the constraints in place.
UPDATE :
From the screenshot, it appears that you are using Distribution = Fill Proportionally for the StackView, and it will require a height constraint to distribute it's children proportionally within that height. You should use Distribution = Fill and it will NOT require the height constraint anymore.
UPDATE 2 :
According to the 2nd screenshot, your ScrollView height is not defined. You should set something like ScrollView.bottom = superview.bottom which will tell ScrollView how tall it needs to be. Once your ScrollView height is defined, it should go away.

How can I add space above and below an ImageView in a row in a TableView?

The row height in this table view is 54. The ImageView Height is 44. I'd like 5 above and below the ImageView (some spacing the image so that they aren't touching each other). Basically, centering the ImageView, vertically, in the row (or ContentView). No matter what I've tried, the image is always anchored in the top left corner of the row (since its a music app, it has to run on a phone, so I don't have an image for the result). I'm embarrassed to say that I've spent hours trying to accomplish this. I'd appreciate any help. Thanks.
If I were you, this is how I would set up my constraints. First, I would set the width constraint on the image view to be 44. [Note: I set my row height to be 54 just like yours.] Then I would set the aspect ratio constraint on the image view to be 1:1 (assuming you want a square image). Then, I would put the image view and the label to be in a horizontal stack view. If you bundle everything up in a stack view, I find that you have to worry about less constraints (the stack view will take care of the spacing between the image view and label although you can adjust accordingly if needed). Then I would add leading and trailing constraints to the stack view. And then finally, center the stack view vertically in the container.
Hope this helped. Good luck!
I was right, there was another place that was forcing the image into the top left corner. It was in the code when setting the contents of a row as shown below. I wrote that part a long time ago when I knew even less about Swift (the goal was to make sure the image gets scaled. It may not be necessary though. I'll test the code without it. Still, thank you Zaya for taking the time to help!
let albumArtwork = rowItem.representativeItem?.artwork
let tableImageSize = CGSize(width: 10, height: 10) // This doesn't matter, it gets resized below
let cellImage: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 44, height: 44)) // The albumImageView is 44 by 44

IOS adaptive layout using Autolayout

I am working on IOS app and its my first app to be build on IOS. I have good experience of developing Android apps. So I have gathered basic information and knowledge about IOS development. I am working on autolayout. But I am not getting it work completely. I have seen Some developers are working on code, But I just want to use IB as I believe there will be some way to handle this.
So looking for help, Please do not down vote my this post As I am posting this after trying almost everything I get on internet.
First take a look at what I have developed and what it is :
in picture there are different numbers of simulator such as Ipad pro 12.9, Ipone 8 plus and I phone se
What I have done so far:
I have taken the View (the white box in middle). I have made it center vertically and center horizontally.
I have set view height as 300 points, and width to match the superview with some margins
I have put the Text Fields for taking Phone and PIN from user and OK button as shown in picture.
Problem & what I want:
- The problem is view is center vertical and horizontal but not its
content are. I want this view to manage its height on the basis of
its content inside. so that It can be looked as center aligned
vertically. Right now its not center align vertically
- Secondly the problem is the width of text fields, as on Ipad they looks too bad. So I really want them to be look good on all devices.
Is there any solution for this using the adaptive/Auto layout? please help me that How can I get this using IB.
Try following solution:-
Fix the width of textfield.
For label use minimum font scale(search on google).
For width and height you have to use adaptive layout.
width = 70% of superview which means multiplier for your view will be 0.7
height = 60% of superview which means multiplier for your view will be 0.6
Refer to the image:-
Equal width with the superview
Set the multiplier as 0.7
#Android Teem,
For this you need to use SizeClasses along with the AutoLayout.
Below I am trying to give you solution.
use items structure as like below. Don't use another UIView inside main view. Instead user StackView. It automatically adjust contents inside it. For more just look for "UIStackView" on Apple Doc.
Next you need to add constraints to stack view as
Leading 10 (for x)
Trailing 10 (for y)
Allign to center (for width)
height stack view will automatically adjust.
Now double click on leading and trailing constraint will open size attributes. Click on "+" icon just before Constant. It will show you a pop up. add Regular Width Regular Height for iPad and set values as 200 (I have give 200, you can give based on your calculation and requirement). this will show like below image
Now Either run or check from preview for all screens. you will get desired results. my changes show result as below
Happy Coding
Here is what you need to do. Go to the storyboard
Right click on the white area of the screen and drag the cursor to the green area.
Few options will appear, select Equal Heights
Now the white dialog box will become equal height to the green screen but don't worry. Select that height constraint and set the
multiplier to let's say 0.5 for 50% height of super view. Repeat the
same for width
Remove the height constraint and hook the bottom most button to the bottom of the view

ContentSize Height of UIScrollView in iPhone X not updating

This issue sounds easy at first but this is one of those things that I had to ask a question for suggestions since there is no other way I can think of to solve this. I already searched too far for solutions but did not find any.
Situation
I have a collection view that scrolls horizontally that covers the whole screen.
I added a scroll view inside each cell's content view that contains several views that will be taller than screen height.
After the last subview is added I update scroll view content size using:
scrollview.contentSize = CGSize.init(width: screenWidth, height: (scrollview.subviews.last?.frame.origin.y)! + (scrollview.subviews.last?.frame.size.height)!)
Let's say "scrollview" is the scroll view and "screenWidth" is the width of the screen. It works on other devices except iPhone X will scroll maybe for two screen heights worth but that's it. Other devices can go to the bottom of the scroll view.
While writing this I thought:
what if I need to add top and bottom padding of safe area to content size height since it extends there.
I did it but it only added so little and a big part of views still cannot be seen due to this issue.
Inputs are welcomed. Thanks in advance.

Storyboard constraints using ScrollView aren't respected

I'm quite new to iOS, specially to complex views using Storyboards. I've started to develop the following screen trying to use AutoLayout so I don't need to worry about different iDevices screen sizes.
I'm using ScrollView to move the content to visible space while need to input some info with the keyboard. The thing is I re-created all the constraints a lot of times trying to make the fields respect the width of the ScrollView. Actually, when running, all the fields are being displayed larger than the ScrollView width (it's like overflowing the screen width to the right). The curious thing is that the button respects its width. When I change the orientation to landscape on iPhone 6s Plus those fields has smaller width than the screen width as if I had defined a specific size and the button continue to respect the size according to it constraints. Any thoughts?
Hierarchy:
View Controller
View A
Scroll View
View B
Logo / App name / UITextFields / Switch / Button / Link
Basically the Scroll is constrained with View A. View B is constrained by equal width with to View A and to Scroll margins.
Current Xcode version: 7.2 (7C68)
Please find the constraints added to scrollview i.e top,left,right,bottom constraints.
At the same way set constraints for individual elements inside scrollview i.e textfield and button. For these elements , set left,right,top and height constraints. I have added in my code to achieve your functionality see screenshot below.
Scroll view constraint - top,bottom,leading,trailing ,
View B constraint - top,bottom,leading,trailing and must height-width constraint set ,
Other element - leading, centrally vertical and must height-width ratio
please try, this will help to solve your issue.
First I would like to thanks Bharath and Dhaval for their answers, they were really helpful.
Although the answers helped me to have a better behavior in this view, it turned out that my problem weren't related to the ScrollView but with a mask I'm applying to those fields and the moment where those mask were applied.
I was applying these mask to get a customized round corner so I had the corners exactly like the post image:
// Change webservice's text field style
CAShapeLayer *shape = [[CAShapeLayer alloc] initWithLayer:self.url.layer.mask];
UIBezierPath *shadow = [UIBezierPath bezierPathWithRoundedRect:self.url.bounds byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(5, 5)];
shape.path = shadow.CGPath;
self.url.layer.mask = shape;
But this mask were applied in viewDidLoad. Changing to viewDidLayoutSubviews solved my problem and I get the right design and behavior in portrait or landscape: round corners where they should be round and respecting the constraints.
Two other sources from stackoverflow I didn't found before and may be useful for people having problem with ScrollView:
https://stackoverflow.com/questions/31015026/autolayout-and-uiscrollview-storyboard-constraints-are-not-respected
UIScrollView Scrollable Content Size Ambiguity

Resources