Autolayout a view within a view - ios

I'm trying to have auto layout for my ios application but for iphone 6s the border is too small I've tried using pin and alignment constraints but it doesnt look right. The iphone 6s plus looks fine though. The only problem I have is the iphone 6s. The third image is what I have right now which is the view has is horizontal and vertical in container.

It looks like you're using fixed dimensions for the cells and the inter-cell spacing. Using a UICollectionView is probably overkill for this example, because you'll always have 9 squares for Tic-Tac-Toe. If that's the case, then you can do the following, assuming that the squares are UIButtons and they're contained in a plain UIView:
Constrain one cell's height and width to be 1/3rd the view's height and width. You can do this in the storyboard by adding height and width constraints that are equal to the superview's, but with a multiplier of 0.33.
Constrain all other cells' heights and widths to be equal to cell #1's.
For the borders, use User Defined Runtime Attributes to add a border, like this:

Related

UITextView dynamic width according to text inside in UITableViewCell

I'm working to make a chat bubble like in iMessage. I have to use UITextView to get advantage of DataDetectors to detect urls, phone numbers and addresses automatically. My view hierarchy is like;
-UITableViewCell
-ContentView
-ChatBubbleContainerView
-UITextView
ChatBubbleContainerView have constraints of
10-pt left, right, top and bottom margins --> ContainerView
UITextView have constraints of
10-pt of left, right, top and bottom margins --> ChatBubbleContainerView
I want to achieve a behaviour to make the text view automatically expand its height and width according to its content inside e.g. text. But the text view has no intrinsic content size.
These are what i have tried and got wrong results
Overriding the intrinsicContentSize property of text view but it's
not working too.
Giving a width constraint to the ChatBubbleContainerView and changing it to result of sizeThatFits() method
Giving a width constraint to the ChatBubbleContainerView and changing it to result of text.boundingRect method
These are the results i've got
It's not working properly and the cell is not laying out as expected as i scroll the table view
It's working but the initial width of cell in the nib is 375-pt which is good for 4.7 inch devices but when i switch to devices like iPhone SE which has 4 inches at the initial table view load or cell insertion, the cell's width is calculating according to 375-pt not 320-pt. After scrolling cell, the layout is laying out as expected.
Same with 2.
When i try same layout with UILabel it's working as expected. As you may know UILabel has intrinsic content size.
Thanks
TextView Contraints are good you need to made some changes on ChatBubbleContainerView constraints.
ChatBubbleContainerView constraint:
top, leading, bottom -> 10 pts
MaxWidth(GreaterThanOrEqual) to contentView, for instance 0.85 or you can set TrailingConstraint with maxConstant(GreaterThanOrEqual to contentView.
you need to set the maxWidth then only ChatBubbleContainerView will be responsive.

autolayout view gets overlapped on smaller devices

Basically I have one view with some height x
and 4 other views with same height y all these 5 views are vertically one after another with 10 px space
all these views have again some child views which depends on them.
For this scenario stackview is ideal but it support starts from iOS 9 unfortunately I have to support from iOS 6
so basically I fixed all the views with leading,trailing,top,bottom and height constraint...
it works good in iPhone 7s ... but in iphone 4s the view gets overlapped on each other
in iphone se the views are very much congested..
I was always thinking the height will change according to the screen size for iPHone 4s but that didn't happen
How can I work this out ?
There are two ways to solve the problem you are facing.
First:
Instead of giving a fixed height to any of your UIView you can use a proportional height constraint. Select your UIView and control + drag from your UIView to the main superview. Select equal height constraint. Now, double click the height constraint and set the multiplier to 0.15 or any value you seem nice. This will ensure the view thus created is always 0.15 times the entire view's height. Now you can create your other views either proportional to this view following the same steps or to the superview.
Second:
(I prefer this approach as for items as sometimes you need to create forms the above approach might still push elements off the screen).
This will use a UIScrollView. To your main view add a UIScrollView and add a leading, trailing, top and bottom constraint to your superview. Add a UIView to this UIScrollView and give it a leading, trailing, top and bottom as well. Additionally give it a equal height and width constraints to the superview of the UIScrollView with a low priority of like say 250. Now add all your elements inside this UIView however you seem fit. With fixed height, proportional height whatever and happily run it. But ensure you add a bottom of >= a minimum value for your bottom most view.
The screen will automatically become scrollable if the content will go off screen otherwise it won't be scrollable at all.
fixed all the views with leading,trailing,top,bottom and equal height constraint instead of only height(not give fixed height to any view)

Square UImageView in a Cell of UITableView

I'm trying to place an ImageView in a Cell, I want the Image to be square and to work on most iOS phones, iPhones and iPads.
I'm using constraints to do that, I made an aspect ratio but it doesn't seem to work right.
Do you have any idea how to do that ?
As said by Vladimir in the answer Auto Layout: Square Image View with equal width / height
you need to set this constrain to your imageview
First of all try to switch mode from w:Compact/h:Any to
w:Any/h:Any. By adding constraints in this mode you will apply them for all devices and orientations.
Here is list of constraints that you need to apply:
Set low priority for Equal Width and Equal Height to superview.

How to Configure AutoLayout for UIView

I have a UIView in my iOS app that holds a graph. I have fully constrained the UIView in Interface Builder but yet when I view the page on my iPhone 6 the graph is a nice large size but yet when viewed on an iPhone 4s, the view appears much shorter to the point where it's uncomfortable to look at.
The constraints are as follows:
As viewed on an iPhone 4s:
As viewed on an iPhone 6:
How can I adjust the constraints so that the iPhone 4s UIView has similar proportions to that of the iPhone 6?
There might be some conflict in the constraints in 4S.
For this UIView, if the superview is fixed in terms of size, then only 2 of the following constraints need to be set.
align top to superview
align bottom to superview
height equals 150
Or if the superview is not fixed in size, then the superview constraints might have conflict with the tableview down below. try to set the tableview height using percentage, not the fix number?

Cannot get view to use entire screen width on iPhone 6+

In my storyboard I have a MapView that I would like to use the entire width of the screen. I created two constraints for the leading and the trailing space and they are set up as follows:
On the iPhone 4S - 6 the view works as expected, with the map using the entire screen width. But on the 6+ I can see that the map isn't using the entire width and has about 5px of padding on both sides, as seen here - it's hard to see with the background being white:
How can I make this mapview use the entire width of the screen for the 6+?
Edit the constraints so that they're not using the margins (do this in the size inspector for each constraint, from the item drop down) then set the constants to 0. It looks like the margins are bigger on the 6 plus, so using -16 to offset isn't giving you the right effect.
Only use constraints relative to the margin if you actually want a margin. I've no idea why this is now the default, it's stupid.

Resources