I have a label with many lines of text. I want to have 20px of margin on left and right if the screen is small but I do not want it to be bigger than 300px if the screen is big.
What I have tried:
- I choose a label as I want the text to be not scrollable and not selectable
- I have given it Attributes > Lines > 0. To adapt to many lines
- I have given it constraints to top, left and right
I am very new to Xcode. In html I would use something like that css:
.text {
with:90%;
max-width:300px;
}
How could I create a similar effect in Xcode and Swift?
(The left and right constrains works well for me to give a similar effect to the width:90% in a small screen. What I am asking now is how to limit the width of the label if the screen is big. Please, be aware that I am very new to Xcode)
This is where you'd want to use less than and greater than constraints.
Set the following constraints:
width ≤ 300
left ≥ safe area left + 20
right ≥ safe area right - 20
horizontally in container = 0
top = 20
You can add a ≤ or ≥ constraint by selecting the constraint and using the drop down that says "Relation" in the property inspector:
Related
I'm using ag-grid-community v.25.1.0 with 2-rows header and 5 pinned bottom rows.
I want the grid header to be always at fixed position - even when I scroll to the last data rows of my grid (which are often far below the screen bottom).
Unfortunately this forces me to set 'height = 100vh' for the grid - but then the 5 pinned bottom rows also occupy screen area, so there's too small height left for the data rows (about 10 data rows left when I'm using my 13-inch laptop). See demo #1 at https://codesandbox.io/s/aggrid-issue-demo-j9ls9
And if I do not limit grid height with 100vh - then the pinned bottom rows go ouside the screen together with the data rows (because AgGrid's OWN vertical scrollbar is no more shown) which is OK except for... scrolling down the vertical scrollbar of the grid's container (body tag) moves grid's header outside the screen (although "I want header to be always at fixed position" - see this requirement above!). See demo #2 at https://codesandbox.io/s/aggrid-issue-demo-2-52bo3 - it would be OK if header stayed at fixed position.
If there were no pinned bottom rows - the problem would not exist. But these pinned rows are really necessary in my app; however the fact that they occupy ~33% of the grid's height - is a real problem. Yes, I can make row height smaller (together with the font used to paint the cells) - but that's an ugly workaround, not a real solution. I can also provide users with a possibility to show/hide some of the pinned bottom rows (using several checkboxes) - but this "solution" also smells (besides, in most cases all 5 pinned rows are needed).
Is there any solution? maybe I can adjust header's CSS so that it's Y-coordinate stays strictly fixed? in such case removing 'height = 100vh' would solve my problem...
UPD: maybe it makes sense to describe the difference between demos #1 and #2:
App.css:
added this file to override the ".ag-root-wrapper-body.ag-layout-normal" style - because it had height set to zero
App.tsx:
added import for App.css (see above)
removed div style "height: '100vh'"
index.html:
removed body style "overflow-y: hidden;" (to enable vertical scrollbar which is needed to access last rows after we removed '100vh' above)
I am new to swift and so I am not familiar with auto layout. I have an image. on the right of the image is a label. The distance to the right label should be 8. Also I have a label under the image. Here the distance should also be 8.
My hope was that the picture is doing its size now automatically. But on the picture you can see that it tells me that there is a problem with constraints. Probably because I dont told the image to always make the picture in the view as wide as possible and than look how high it needs to be. But I also dont know how do add a constraint like this. Can you help me?
Constraints on image top left:
scissor.top = topMargin (so 0 to top)
scissor.leading = leadingMargin (so 0 to left)
constraint between scissor and label on bottom:
label.top =scissor.bottom + 8 (so distance of 8 between them)
constraints of label on bottom:
label.bottom = bottomMargin (distance of 0 to bottom)
label.leading = leading.Margin (so dostance of 0 to the left)
constraint between label on right and scissor:
articleLable.leading = scissor.trailing + 8 (so distance of 8)
constraint of label on right:
articleLable.top = topMargin
articleLable.trailing = trailingMargin
You need to constrain the imageView, because x-Code does not know what 'as wide as possible' means.
I assume you mean that the left side of the imageView and the Cell's left side are identical and the top side of the imageView and the Cell are identical.
Adding these two constraints should fix the issue.
you can click on red icon like image for detail
Image
I just begin swift and I'm facing a simple problem:
I can't adapt a UIButton to an image in my storyboard for all iPhone sizes. I work with a storyboard with the size of an iPhone 6s Plus because I have one. When I run my test, all it's OK but when I run my test with iPhone 5 simulator, nothing is good.
This image shows the button is in the good place on iPhone 6 plus but no to other sizes.
I tried so many different constraints but nothing seem to work.
In summary: How can I set constraints to position a button at the circle in the background image (as shown in the linked drawing), regardless of phone size?
Try setting your constraints to a percentage.
Control-drag from your button to your view.
Shift-click to select Horizontal Spacing and Vertical Spacing.
Click Add Constraints.
Now, you should have 2 constraints for the button, relative to the superview.
In the Attributes Inspector for each constraint, check to ensure the button's X and Y are relative to the trailing edge and bottom of your superview. For example:
First Item: Button.Center X
Relation: Equal
Second Item: Superview.Trailing
First Item: Button.Center Y
Relation: Equal
Second Item: Superview.Bottom
In each constraint, set the constant value to zero.
Then, set the multiplier value to a percentage or fraction.
For example, if you want the button to be at the halfway point, regardless of device size, enter a multiplier of 50% (or 1/2 or .5 if it won't take the percent value - sometimes it won't accept one version of a value, but it will accept another expression of the same thing).
The percentage is multiplied by the superview's values. The superview's trailing edge is the max X value, and the superview's bottom edge is the max Y value.
Your button appears to be around a third of the way down the view, and about a quarter of the view on the left. So you might try setting the Button.Center X multiplier to 25% or 1/4 or .25, and the Y multiplier to 33% or 1/3 or .33
Note also that under a constraint's Second Item dropdown, you can select whether the value is relative to the margin. If that is not selected, the multiplier uses the full width or height of the superview. Selecting Relative to margin uses the width of the superview minus any trailing or bottom margin. That could throw off your percentages. I recommend leaving Relative to margin unselected when playing with your values.
Here are images for the end constraint values:
Note that the button doesn't seem to be centered until you look at the preview. Running the simulators for the different sized phones gave me the same results as shown in the preview: button correctly centered in the circle.
I am doing a sample project with size class and autolayout in swift.I have four buttons on top of the screen.How to set same width for all buttons with one pixel gap between them and make them fit for all screen size.
thanks in advance
The trick here is to make the button widths variable, but the same for all buttons. This can be accomplished like this:
Place the four buttons
Set the left margin constraint from the leftmost button
Set the right margin constraint from the rightmost button
Set horziontal distance constraints between the buttons to 1
Set the width constraints for all buttons to >= 10 (or any other
value, this makes the width variable)
Set equal width constraints on all buttons (ctrl-drag from one button to the next and choose equal width. Repeat that for all buttons)
I'm trying to build an app that has just a single portrait oriented view. There is a title on top and results label at the bottom of the view, these are locked to top and bottom and center, no problem here.
Now this is the tricky part. I have 9 rows of labels, most single row but couple span out to 2 rows. I want them to spread out evenly between the title and the result-label.
I lock the first label to top-left, lock the leading edges and then set equal vertical distance constraints between the labels. Otherwise ok but the last labels on 3.5 inch display overrun my results -label.
So I adjust either the fontsize or make vertical distance between labels smaller or both. 3.5 inch problem fixed but now there is a big empty gap between the last label and result. On iPhone 6 its really ugly.
I have tried all sorts of methods but can't just get this working.
So basically: how do I reserve 10 % of view height from top and 20 % from bottom and tell Xcode to use the rest 70 % (and all of it) for my 9 rows of label, evenly?
Thanks for help!
Would have posted an image but no rank for such things...
If I understand you right, you need several flexible spaces that will all be the same size. One space in-between each of the labels.
Create UIViews to put between each label, set them all to have equal widths or heights and anchor each one to the views it is supposed to sit between.
I got this idea from another SO question: Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5