macOS NSStackView content does not fill the available space - macos

I use a NSStackView to dynamically (added by code) display other NSViews, everything works so far, but the sub views are to small.
They try to be as small as possible, regardless the constrains, if i resize the views in IB, they scale correctly
The Header with the hide button does not fill the available space:
I did set the translatesAutoresizingMaskIntoConstraints property to false on the stackview and all subviews

Well, it's hard to know for sure without more information, but the most likely cause is that the horizontal content hugging priority of the labels is higher than the horizontal hugging priority of the stack view. (Note: the former is a general property of all views while the latter is a stack-view-specific property.)
You also need to have constraints between the stack view and its superview to make the stack view stretch the full width. For example, leading and trailing space constraints.

This is quite old, but in modern times I have found that if you set the alignment to width, everything magically works.
stackView.orientation = .vertical
stackView.alignment = .width
stackView.addArrangedSubview(nowFullWidthSubview)

Related

How to keep fonts the same size across (where possible) different controls with autolayout?

I have several UILabels which span a different number of lines. I've set the font / size for each with different sizing classes. I've also set a minimum size.
However I can't keep fonts the same size.
How do I achieve this with autolayout?
I don't know how many UILabels you have. That's why you can start with adding a UIScrollView to your view controller that enables dynamic content. (or you can make it by using UITableView)
Here you can follow the steps to create UIScrollView with dynamic content.
After that, you should add your UILabels with bottom, top, trailing and leading constraints. It will resize label height regarding to your text length. Also font and size remain same.
A friend suggested a UIStackView, which is awesome

two labels horizontally in tableview?

I would like to have two labels in my tableview.
but the label resize option is disabled.
How to have the two labels horizontally?
Select UIlabel instead of the cell and then you can set Auto resize like follow.
You may need AutoLayout and Custom Tableview Call. Use two different labels, e.g. one for username (left aligned text) and another for garbage text (right aligned text).
Now, set Content Compression Resistance Priority for one of both label, according to your requirement for data visibility when there is long texts in user name.
Look at these snapshots:
Labels with default content compression resistance priority
I changed content compression resistance priority for label blablabla blablabla, from 750 to 749.
Result is:
For more details see Apple document: Setting Content-Hugging and Compression-Resistance Priorities
Drag a horizontal UIStackView from object library and set it's constraints like this inside the cell
//
drag 2 labels inside it and make the distribution .FillEqually
//
Are you sure you have selected Label?
If you know about Layout constraints, it will be easier than this auto resizer.
Label 1==> Set Leading, Top & Bottom constraints to SuperView. Set Trailing constraint to Label2.
Label2==> Set Trailing, Top & Bottom constraints to SuperView. Set Trailing constraint to Label2.
Now, after this, it will show Red error arrow. Now, you have to decide which Lable width is a priority. After deciding, select that Lable and set Horizontal Content Hugging to 251(High) and Comprehensive to 751(High). Also, change lower priority label Content Hugging and Comprehensive to 249 and 749 respectively. Now, Red error arrow will not be shown and in the cell it will show all text in both labels without and cut.
if you want a easy and quick fix for it, implement a stack view inside the tableview cell. You can change the stackview as you want and also you dont have to worry about applying constraints.

How do I get the auto-layout to work correctly in xcode?

I am attempting to get the auto-layout to work correctly. For some reason, when I change the size of the device, all of the images and buttons are out of place and don't resize at all. This is true for the 3.5 inch screen, as well as the ipad retina. When I run the simulator, the app shows exactly how it shows in storyboard. Auto-layout should be resizing everything so that this does not happen. please help!
The issue is that constraints don't scale. When you set a constraint height to 200 it will always be 200, even if the screen is only 100 high. So what you need to do is try to set constraints that makes the size dynamically rather then set the constraint height/widths yourself.
For instance, on your first label under the UIImageView set the constraints leading and trailing to 20, then set the label constraint to center horizontally on the UIImageView. No other constraints for the label. This will cause the label to shrink in width since its set to be 20 away from each side. The height will not change since there is nothing "pushing" it to shrink since you are missing a few constraints, but basically you need to use the top, bottom, leading, and trailing, constraints to the UIViewController view in order to "scale" the elements.
If you'd like I could try and set the constraints for you if you upload the project and you can then watch how I did.
Edit
It's hard for me to know what kind of behaviour you'd want, but something has to scale or change size in your set up. The easiest would be to resize the UIImageView. It's basically impossible to make it look exactly alike in every screen size, though, so you'll have to play around a bit with constraints until you understand how they work and how to adjust them according to what you want. One way is to create the layout you want and then connect them to code and scale them depending on size of the screen (this is a bit of a pain, I did that in a project recently and it took a bit of time and effort).
The easiest approach is to make the UIImageView to change its size when it needs to, and add extra space in the bottom. Here is a demo project I threw together earlier. Download here
Profile pic
1) first you set the profile pic image center horizontal to the content view
2) set height and width for the profile pic
3)set top space constraints to content view
Change Profile pic label
1)set central horizontal to profile pic
2)set width and height
3)set vertical space between profile pic
Challenges Accepted label
1) set vertical space to profile pic label
2) set Leading space to container
3) set height
and do same for the below two content . i didnt tested. check it and try
After a long night of struggling, I figured out what I needed to do. I was setting the individual view controllers to have a simulated size of 4.7inch. This made it hard for the constraints to scale across all devices since I was setting the constraints for a specific screen size. However, this would be the way to go if using size classes.
I solved my issue by using the standard 600 X 600 canvas that is available before choosing a specific size. This way, I was able to set constraints that scaled down for the smaller screens, and scaled up for the larger/HD screens. This works perfectly with auto-layout. Hope that helps someone!

Xcode: difference between BACKSPACE + CMD and BACKSPACE?

I don't understand the difference between using BACKSPACE+CMD and just BACKSPACE.
I see that when I use BACKSPACE+CMD on a element in the Storyboard, that element becomes opaque, and using just BACKSPACE it deletes the element. I can't find the answer in the documentation.
As far I know, when opaque, it means the component (constraint or UI element) has been unistalled for a particular size class. For me, the combination is Command + Delete.
From About Designing for Multiple Size Classes (in bold the key part).
With size classes, a storyboard or xib file can be used for any
available screen area. You build your interface as it will look in
most sizes, then update only the parts that need to change when the
available screen size changes.
A size class identifies a relative amount of display space for the
height and for the width. Each dimension can be either compact, for
example, the height of an iPhone in landscape orientation, or regular,
for example, the height or width of an iPad. Because much of the
layout of an app does not need to change for any available screen
size, there is an additional value, any.
Hope it helps.
P.S. Did you check the key binding for your Xcode?

Scrollview in Interface Builder with Autolayout with content size dynamically set to screen size?

I want to set the content view of a scrollview to whatever the current screen size is, but AutoLayout is doing some funky stuff. This is trivial in code... just create a scrollview with a frame that is the superview's bounds. Then create a content view with the scrollview bounds and populate it and set the alwaysBounces... properties to YES. In interface builder though, this is some kind of sinful thing it seems.
I know Autolayout handles scrollviews totally differently because it wants to infer the content size based on constraints. My approach that failed is setting the scrollview to have 0 distance to its superview (all sides attached). Then, the same with the content view (the single scrollView subview)- attached to all superview edges. Then the precompiler thing complains about not knowing the content size, so I set a width and height constraint at placeholder to be removed at build time. But the result is a (CGRect){0,0,0,0} contentView. The 0 space to trailing edge and bottom of superview are totally ignored.
So how can I make a scrollview with a dynamic content size based on the screen size?
Bonus points if you can explain how you would do the same, but for a content size of 2x screen width.
You can try setting a constraint for the content view's width to equal the scroll view's width (for your bonus question: with a multiplier of 2). Same for height.
Not sure what the point of having a scroll view whose content is always the size of the scroll view is. By definition it wouldn't scroll, right?

Resources