iOS scroll view bounds not equal to superview bounds - ios

I've got a UIScrollView that has constraints pinning it to the superview's leading and trailing edges (not the margins). In this scroll view I have a UIImage which I'm setting to initially be the full width of the scroll view. On the iPhone 7 this works correctly, the image is full width
However on the iPhone 7 Plus there is a gap between the edge of the image and the edge of the screen. The scroll view is full width as when I zoom the image in it does go right to the edge.
I'm getting the image to match the scroll view width by setting
imageView.frame = scrollView.bounds
imageView.contentMode = .scaleAspectFill
The issue seems to be the scroll view bounds. On the iPhone 7 Plus the scroll view width is reported as 375, however the screen width is reported as 414. On the iPhone 7 the scroll view width is 375 and the screen width is also 375.
Anyone know why this is? I'm sure I'm misunderstanding something!

Most probably your problem is that scrollView's frame is not updated yet when you use it to set imageView's frame.
Try to move layout code into viewDidLayoutSubviews instead:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
imageView.frame = scrollView.bounds
}

Related

XCode: Scrollview not scrolling

so I have added scrollview to my screen.
However, I'm unable to scroll up and down.
My scrollview width and size are 414 and 784 respectively while my content size is scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 800)
Why can't I scroll up and down?
How do I fix this?
You can add a UIView (call it content view) inside your scrollView with top, botttom, left, right constraints to zero. And give height constraint = 800 for this view.
This should work!
Basically, you need to add some content to the scrollView. If the height of contents becomes greater than the scrollView height, the scrollView becomes scrollable.

UIScrollView cannot have correct contentSize

I have 4 UIViews in my UIViewController with colours: Yellow, Green, Grey and Blue.
I also gave the following auto layout constraints:
Yellow view: Top-8-superview, Leading-8-superview, Trailing-8-superview, height = 120
Green View: Top-8-YellowView, Leading-8-superview, width=200, height=100
Grey View:Top-8-GreenView, Leading-8-superview, Trailing-8-superview, bottom-8-blueView;
BlueView: Bottom-60-superview, trailing-8-superview, width=260, heigth=30
After compile and run, it looks like this:
At this point, no problem, no constraint complaints, everything is fine.
However, I changed the 4 UIViews' parent view to UIScrollView, and then the UI display wrongly: only green view displays correctly, yellow view and grey view are missing, blue view shows a small part.
In the console I checked that the scrollView's contentSize.width is 16, which is incorrect, so I tried to correct it in viewDidLayoutSubviews:
self.scrollView.contentSize = self.view.bonds.size;
[self.scrollView setNeedsDisplay];
[self.scrollView layoutIfNeeded];
The contentSize becomes (414, 736), which is correct, but the display is still same as my second attached image. I listed out the view details:
YellowView: frame:(8, 28, 0,120)
GreenView: frame:(8, 156, 200,120)
GreyView: frame:(8, 264, 0, 274)
BlueView: frame:(-252, 646, 260,30)
What I observe is, if I give a width constant, the view is at least has width greater than 0, and for the Yellow and Grey view, since they are blank view with no width constrains, they will have problem to show.
What is the best solution to fix it other than give width constraints? I am also wondering why the width of yellow and grey is zero, since I also gave top, leading, trailing, height constraints?
Add contentView as subview of scrollView and then all your subviews as subviews of contentView. Constraints for contentView set as width = superview.width, height >= superview.height and pin 4 sides of contentView to scrollView (leading, top, trailing, bottom). More info: https://www.natashatherobot.com/ios-autolayout-scrollview/

UIView for Table View Header Width Wrong

I have a UIView as my tableview header but the width is slightly smaller than the simulator for some reason. I haven't been able to fix this. The tableview is inside of a view controller and the UIView has been allocated like this:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 40.0)];
The width is showing 375 instead of 400 as it is supposed to be so it is slightly smaller in the iPhone 6 simulator and on my iPhone 6.
I know the width of the iPhone 6 itself is 375, but if you see the view's width in the size inspector it says 400 as you can see in the image below. Also you can see how the UIView is slightly smaller than the UITableView width. This could be due to some spacing issue also, I am not sure. The constraints added to the uitableview were 0 spacing on the top and bottom and -20 on the left and right.
You should set your frame in:
viewDidLayoutSubviews
Because in it frame is correct.
I had a feeling it was the -20 spacing that I had on my tableview. I wasn't sure how to make it 0, but walk_alone suggested unchecking constrain to margins and this allowed me to set the spacing to 0 for both of the side constraints. This allowed for the UIView to take up the full width.

How to resize a fixed width and height UIImageView with auto layout

I have a view named myView which is always half the screen and sit at the top of screen.
Inside this view I added an UIImageView with 120x120 size which sits in center of the myView (horizontally and vertically). Inside the IB, to satisfy the constraints(X & Y) I always need to set a fixed width and height for my image, after I set center horizontally and center vertically.
But with a fixed height and width, the image doesn't resize when changing the screen size. I want my image to resize when running on iPhone5 or iPhone 4s, because myView will resize.
I need something like the image should depend on the myView size.
How to actually achieve this ?
set imageView.clipToBounds = yes, and also set imageView.contentMode = UIViewContentModeScaleAspectFit;
Update
You are talking about imageView not the image, in that case it will not resize due to constant width and height, do one thing, create IBoutLet of constraint and change there values when required, or you can also set Aspect ratio with superView
If you set the image with fixed width and height it will not change respectively.
You can either set it to be relative to myView or you can instead set leading and trailing size from all for edges and delete the center constrains.
Your constraints should look like this:
For Swift 4: Updating answer of #Adnan Aftab
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit

UIScrollView contentSize height equal its frame height, why there is still a scroll bar?

I config scrollView contentSize same as its frame height(I also test <), Why scorll bar still shown, it seems that the scrollview content size is not what I configed in code.
I think you are place scroll view in view with size for Retina 4 inch, and scroll view automaticaly resized.
add line in viewDidLoad method
self.mainScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
or set up right autoresizing mask in IB.

Resources