UIButton inside UIScrollview not resizing with UIScrollView even after adding the constraints - ios

I am facing a weird issue. I have implemented custom UIButtons as a Tile View in a UIScrollView which is added to superview. It is like there are six buttons, set of two placed sideways, and next set of 2 below them and so on. Now for implementing it to fit all iPhones Screen Sizes, I have added proper constraints to UIScrollView and constraints to UIButton relative to each other and UIScrollView. I have added #2x background images to each of the button with resolution as 320 *320. I have not provided any heights and widths constraints to them, so that they stretch freely according to different screen sizes.
xib has been initially designed as a 4" screen size. UIScrollView frame is stretching, however button remain positioned as in an iPhone 5S xib, hence it is looking improper on screens like iphone 6 etc.
What is the exact problem here?

See the work done here and confirm if this is correct. I have added constraints required to make the buttons looks proper in all devices.
here is the link
https://github.com/mahesh-agrawal/Test-Buttons-Inside-ScrollView

Related

How to have different layouts for iPhone and iPad (Swift)

In an app that I use, there are 4 rows of buttons with each row containing 3 buttons. When I use the same app on an iPad, the alignment of the buttons becomes 2 rows containing 6 buttons each. Also, the width and height of the buttons change depending on the screen size. I want my app to have the same features but I don't know how to do it. Any help?
1) You can use Autolayout constraints and size classes to make the view compatible to both iPhone and iPad screen sizes.
2) You can create and use separate Storyboards or .Xibs each for iPhone and iPad and use accordingly.
3) if the view does not have complex UI, then you can simply manage by doing it via code by detecting the view size, width height etc i.e whether it is iphone or iPad and then setting the frame, width, height of the UIElements like button, label, image view etc.
You need to use size classes. Following is the link which helps you to understand size classes :
Size Classes or Adaptive layout
Use Autolayout for defining constraints on buttons.
There might be two cases :
Either you want your buttons to be of the same height/width; In this case, you can define the height and width constraints to your buttons.
Or you want the buttons to be bigger. For that, you can define autolayout constraints in code if the screen size is for iPad.

Autolayout UIImageview Swift

So,
I am trying to build an app from design, the design consists of 3 images horizontally on a portrait layout
Supporting Iphone 5 and upwards.
From this guide http://iosdesign.ivomynttinen.com/ the Iphone 5 should have 640px of horizontal space.
Each image is 100px by 148px high, therefore I have created added 3x UIImageview and laid them out horizontally.
Now without any constraints I get the following
If I auto constrain I get the following, same if I add constraints manually
Skips out the middle.
If I go to any any constraint , I get an error message in the logger.
This can be a vertical space to top layout constraint, just the one and it breaks.
I assume as images are only 100px wide, I should have plenty of room.
On the UIImageview I have done the following
Set Height and Width
Content is AspectFit
Clip Subviews Selected
Auto re-size selected
I have either not understood App design and the images do not fit, or have done some incorrectly with IB and UIImageview?
Any thoughts on this.
Sorry not a lot of code as it's all Interface builder.
Thanks
Sorry,
My own fault, "reset to auto constraints" and "clear constraints" had a bit of an issue, there was a bunch of constraints still kicking about when I delved in deeper.
In the end deleted then all and then manually added.

Adapt design to all devices with xcode in swift

I'm actually working on an IOS application, and i would like to see a background UIImageView in ScrollView.
My problem is that, i'm using Images.xcassets with this two pictures.
And there is my controller with those constraints.
On iphone6 and Iphone6+ i got this display which is correct
on Iphone5 i got that display with an horizontal scrollView
Can i adapt the background image to the device screen, without the horizontally scroll on iphone5 ?
I'm actually using Xcode 7.0.1 with swift.
UPDATED:
Screen with the same width between my imageview, scrollview and rootview.
The other screen with the programming way using swift.
As it stands, you've added the image as actual content of the scrollview.
Which is fine, if it's supposed to scroll with the scrollview. If you didn't want it to scroll at all you could put it behind the ScrollView.
You can disable horizontal scrolling, better yet, add constraints to the image view, such that it has its width = to the superview of the scrollview, i.e. the view controller's root view.
Once this is done, you may also wish to add "centre" as the content mode for the image view, or whatever you wish, as there will be extra content and scaling will look worse. If your view isn't wide enough for all form factors you may be force to use aspect fill.

Resize UIView inside UIScrollView with AutoLayout for scrolling?

I have a layout as follows(Using AutoLayout,iPad):
The UIScrollView has Top,Bottom,Leading,Trailing space to the superview as 0. The UIView also has all four similar constraints. The second UILabel has its height set to 0 programmatically (since more than a screenful of text is shown here). Also, these four pin constraints have also been set up from the UIView to UIScrollView.
When the XIB is loaded, this UILabel shows long lines of text correctly. But scrolling does not work. This is a screenshot of the running app on an iPad:
The yellow background is set for the outermost view and the grey one for the UIView. If you see carefully at the end of the screenshot, the UILabel's text seems to overflow past the UIView.
I suspect that because the UIView cannot "capture" the size of it's subviews, the UIScrollView cannot calculate it's contentsize too(there's no reason for the scrollbar to work if its content view is only equal to one screen). Why is this happening even when the UILabel is clearly a subview of the UIView? If my suspicions are right, how do I set the UIView's height equal to the actual content size so that my UILabel is properly "contained" in it?
(Setting the UIView's height,width statically is not an option) .
NB: Tested the XIB without the UIView and only a single UILabel (with long text) inside a UIScrollView. Now scrolling works(since, I presume, the scroll view can calculate its content size).
NB2: I also tried the answers in this SO question and this post but my UIView simply wont extend beyond the dimensions of the screen. (I only care about horizontal scrolling)
AutoLayout issues are very hard to explain with text. I was having the same problem. I found the solution in this video. hope this will help you too.
https://www.youtube.com/watch?v=UnQsFlMGDsI
This video demonstrates how to create a UIScrollView which has some
views on the top of the screen and a view on the bottom of the screen
in Autolayout. UIScrollView content size is automatically adjusted to
the size of the screen or to the size of the content in such way that
on smaller devices scrolling is active and on larger devices scrolling
is not active because it is not needed. UIButton below UITextFields
always stays on the bottom of the screen and everything works on all
iPhone resolutions (iPhone 4 / 5 / 6 / 6 Plus).

Auto -Layout UIView for both Retina and non-retina views

I try to put UIView at to bottom of my UI. When i put everything seems to fine , except when i check it with Retina Form Factor the UIView sticked in bottom but height is increased than normal layout:
How can i set UIView with auto-layout for both retina 4 and non retina Screen Size. Also UITextField and other UI Objects have no problem with that issue.
You need to set up your Auto-Layout constraints in a way that accommodates both screen sizes.
In the case of the view you're showing here, adding a height constraint in interface builder to the view should take care of that problem.
Adding a height constraint will make sure that whatever screen size is being used, that view will remain at a constant height.

Resources