UIStackView Weight like Linear layout - swift

How to design the following layout with StackView
I know that it can be easily created without using stackview but is it possible using stackview because i have lots of ui which already designed using stackview how can i add divider between views like above image
For that i set the following constraints
**But run time it shows like **

You can use some simple Autolayout constraints to get this kind of interface with a UIStackView.
The below image describe the hierarchy and constraints that you can apply:
Also the Accept and Reject buttons have Equal Width constraint.
Output screenshot:

You can do that using UIStackViews, here is how:
First, create a subclass of UIView to use as the class of the UIView in between the 2 buttons.
class CustomWidthView: UIView {
override var intrinsicContentSize: CGSize {
return CGSize(width: 1, height: self.frame.height)
}
}
You only need to override intrinsicContentSize.
Now, add your 2 buttons and an empty UIView between them, and set the class of the UIView to be CustomWidthView. Embed your 2 buttons and the view between them in a stack view, and set the position of the stack view properly using the appropriate constraints.
Select the stack view and from Attributes inspector, find the property named Distribution and from the drop down menu next to it, select Fill Proportionally.
To reflect your changes in the UI Builder, select the custom width view and go to Size inspector, go down to the bottom of the list and you'll find a property called Intrinsic Size, change its value to be Placeholder, and from the width drop down menu, select 1.
There may be a better way to achieve this, but this is the one I found for now and I'd like to find a better one.

Since UIStackView uses auto layout to arrange its subviews, you can modify its behavior by creating constraints.
I assume you have three views in the stack: the accept button, the divider, and the cancel button.
Create two constraints:
An equal-width constraint between the accept button and the cancel button.
A fixed-width constraint on the divider (presumably setting its width to 1).
See also this Q&A.

Related

Auto resizable collection view cells in Swift

I want to create collection view cells the width of which changes according to the label in them. The labels of the cells get their text value from an array declared by me. Is there a way to create auto resizable collection view cells which change their length accordingly without using custom classes or pod files?
Try experimenting with UIStackView. I'm pretty sure the elements can be auto justified. You'll likely be able create the layout you want with vertical and horizontal stack views.
If you select multiple views in the interface builder that are arranged horizontally or vertically you can embed them in a stack view.
if you multi select the views you want and press that button in the lower right corner of interface builder you should have an option to embed the views in a UIStack View

Scroll content over fixed background image in swift

I am trying to achieve a scrolling over a fixed image, as you can see on the picture.
I thought I should use a scrollview but I didn't quite get it how to use it and what to include into the scrollview, since the image and the button on the bottom should be fixed. In addition, the content should only be scrollable, when the text is to long.
I think the best solution here would be using a UITableView inside a UIViewController.
You can then set up the cells to make it look exactly the way you want.
For instance the first cell would just be the image, the second cell would be your title, the third one would be your menu bar, and so on.
This may be really useful if you plan to present dynamically the elements inside your UIViewController (for example if you need to use a database, you may want to animate the insertion of the rows only if the content has be downloaded already)
So using a UIViewController, you can just add the UITableView to it and set it up using contraints so that it fills the entire view. Then you can just add your button on the top of your table view, as a subview of your UIViewController's view (not as a subclass of your table view otherwise your button would end up scrolling too).
And again here you would need to add a few constraints to your button to make it look alright!
Just try it like this and let me know if you have any difficulties in the implementation :)
It is very complex to explain, To understand you must know good autolayout
To achieve that you need two scrollview and little math :)
Suppose your headerview height is 200
As you know when using autolayout we need following view hierarchy
Your view Hierarchy should be
--> Main View
--> ScrollView
--> Container View
-->Your HeaderView (200)
-->Content View (Equal height to UIView)
--> UIView (Your tabs like button , followbutton)
--> ScrollView 2
--> ContainerView
-->Subviews
Scroll view 1 Will used wo scroll the headerview and second scrollview will used to scroll other content of scrollview (Like Tab and text content as shown in picture )
Content View (Equal height to UIView) will allow your content view to give height of 200 extra to scroll bottom and your second scrollview will scroll to top which will allow to scroll your other content too
https://media.giphy.com/media/a2A4AQeAIkAhO/giphy.gif

UIStackView resizing my views

Im making a custom keyboard, and wanting a label and a textField centered in the space left over the keyboard, I figured the proper way would be to incorporate them in a UIstackView, and then centering the stack. However, i'm having som issues with the stack resizing my textField, causing whatever text i enter to be clipped.
I tried adding compressionresistance and a number of different solutions, but its clear to me that im missing some information about how UIStackViews work. Usually i make them work, but the whole resizing part I dont understand.
The first two screens are without a stack, adding them as subview to the viewcontrollers view, and the second screenshot shows the stackView resizing my textfield everytime the keyboard is resigned.
How can i stop the stackview from resizing its contained views?
It seems UIStackView is inherently a auto layout component, and when it adds your arranged subviews, it automatically positions them using constraints.
Since the UIStackView is adding constraints of its own, and the best way I found to stop it, was to simply explicitly set the subviews constraits:
textField.widthAnchor.constraint(equalToConstant: 300).isActive = true
Apple doesnt like dynamic StackViews: "In general, you should not use dynamic stack views to simply implement a scratch-built table view clone." Read the
Apple Documentation on UIStackView for details.

Can I use a custom view as a NSSplitView's divider?

I want to use a taller (27pt) view as the divider for a NSSplitView which is vertically-stacked (splitView.isVertical = false). Is there an intended way to do this? If not, is there a common hack?
I was thinking of using a thin divider and listening for mouse drags in the custom view, and using those to programmatically change the divider's location. Is this a valid approach?
An approach that doesn't require listening to mouse drags or any active updating of the view is to use auto layout:
1. Override dividerThickness to return your ideal thickness.
2. Override dividerColor to return clear if your custom view is not opaque and if you don't want the default grey showing through.
If you have a minimum deployment target of 10.11 or later:
3a. Set arrangesAllSubviews to NO on the split view and then add the custom divider view as a subview of the split view. (without setting arrangesAllSubviews to NO, adding the custom divider view would add it as a split pane)
Otherwise, if your minimum deployment target is < 10.11 (or you otherwise cannot set arrangesAllSubviews to NO):
3b. Add a your custom divider as a subview of the container of the split view, but making sure it's higher in subview/z-order
4. Add constraints to position that view where the divider would be, e.g.:
let constraints = [dividerView.topAnchor.constraint(equalTo: topPane.bottomAnchor),
dividerView.bottomAnchor.constraint(equalTo: topPane.bottomAnchor),
dividerView.leadingAnchor.constraint(equalTo: splitView.leadingAnchor),
dividerView.trailingAnchor.constraint(equalTo: splitView.trailingAnchor)]
NSLayoutConstraint.activate(constraints)
Auto layout will take care of updating the position of the divider view when the user drags.

NSScrollView with several TextFields

I'm trying to create simple application (for testing purpose) with NSScrollView and 2 multi-line labels inside it (title and document). I can't use integrated TextView because I need 1 general scroller to scroll all content, not just for one selected TextView.
So generally I need fixed one-line title and document text with shared scroller.
I have no mind how to make this.
I've attached my sample project (only Storyboards) with scoller, you can take a look here: scroller.zip
I'm not sure if this is possible in the designer, but it is possible to set correct constraints in the runtime.
Follow the steps:
In the storyboard select your text field
Set bottom margin constraint (28 in
your example)
Set height constraint (242 in your example)
Connect the height constraint to an outlet in the viewcontroller (for example #IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!)
In the viewcontroller's viewDidLoad method set constraints height to expected height (for example
labelHeightConstraint.constant = 1000)
Run :)
Hope it helps!