convert objc to swift regarding NSDictionaryOfVariableBindings
You just take every view that is listed in NSDictionaryOfVariableBindings
and create a usual swift dictionary containing this view with the variable name as a key.
Example:
NSDictionaryOfVariableBindings(view1, view2, view3)
will become
["view1" : view1, "view2" : view2, "view3" : view3]
So
NSDictionary *views = NSDictionaryOfVariableBindings(subview);
will become
let views = ["subview" : subview]
Objective C equivalent of Swift addConstraints?
NSDictionaryOfVariableBindings
produces variable bindings that match your Objective-C variable names. So you need to write the constraint as
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-16-[thumbnailImageView]-16-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(thumbnailImageView)]];
Or you just skip NSDictionaryOfVairableDBindings
and use:
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-16-[v0]-16-|"
options:0
metrics:nil
views:@{ @"v0" : thumbnailImageView } ];
which is a bit more like the Swift version. (Don't have compiler handy just at the moment so might be some small syntax errors in the second part. Will check ASAP).
convert objc to swift regarding NSDictionaryOfVariableBindings
You just take every view that is listed in NSDictionaryOfVariableBindings
and create a usual swift dictionary containing this view with the variable name as a key.
Example:
NSDictionaryOfVariableBindings(view1, view2, view3)
will become
["view1" : view1, "view2" : view2, "view3" : view3]
So
NSDictionary *views = NSDictionaryOfVariableBindings(subview);
will become
let views = ["subview" : subview]
Width and Height Equal to its superView using autolayout programmatically?
I'm not sure if this is the most efficient way to do it, but it works..
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.translatesAutoresizingMaskIntoConstraints = NO;
// initialize
[coverForScrolView addSubview:button];
NSLayoutConstraint *width =[NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeWidth
relatedBy:0
toItem:coverForScrolView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
NSLayoutConstraint *height =[NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeHeight
relatedBy:0
toItem:coverForScrolView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSLayoutConstraint *top = [NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:coverForScrolView
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.f];
NSLayoutConstraint *leading = [NSLayoutConstraint
constraintWithItem:button
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:coverForScrolView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.f];
[coverForScrolView addConstraint:width];
[coverForScrolView addConstraint:height];
[coverForScrolView addConstraint:top];
[coverForScrolView addConstraint:leading];
How to use Visual Format Language to set constraints in Swift?
Visual Format Language allows you to apply programmatic constraints using visual syntax strings. As per the Apples Documentation, the idea is the text visually matches the layout.
Let's break down the syntax part for better understanding:
H: (Horizontal) //horizontal direction
V: (Vertical) //vertical direction
| (pipe) //superview
- (dash) //standard spacing (generally 8 points)
[] (brackets) //name of the object (uilabel, unbutton, uiview, etc.)
() (parentheses) //size of the object
== equal widths //can be omitted
-16- non standard spacing (16 points)
<= less than or equal to
>= greater than or equal to
@250 priority of the constraint //can have any value between 0 and 1000
Now, in order to apply constraint to a view using visual format language, fir we need to make translatesAutoresizingMaskIntoConstraints
false for the view, on which we are going to apply constraints:
imageView.translatesAutoresizingMaskIntoConstraints = false
then we need to prepare a dictionary for all the views, which are to be used in VFL like:
let viewDictionary = NSDictionaryOfVariableBindings(imageView)
then make horizontal and vertical constraints using Visual Format String as per the rules explained above:
let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[imageView]-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary)
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-[imageView(100)]-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: viewsDictionary)
Now, add these constants to your superview like:
view.addConstraints(horizontalConstraints)
view.addConstarints(verticalConstraints)
PS: If you want to make view's width/height dynamic, you need to create a matrics dictionary, pass it in metrics:
instead of setting it nil
and then using the appropriate keyname for the value. For example:
let metricDict = ["viewHeight":300]
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-[imageView(viewHeight)]-|", options: NSLayoutConstraint.FormatOptions(), metrics: metricDict, views: viewsDictionary)
Custom UIView above UITableView (when user pulls to refresh)
The answer is: use AutoLayout.
Storyboard
You can add all the constraints you need from the storyboard
XIB
If loading a UIView
programmatically, you can also add NSLayoutConstraint
programmatically. They are guaranteed to work in landscape mode.
...but I can't get them working...
Look at https://stackoverflow.com/a/18759148/218152 and add constraints for refreshBackgroundView
height. To pin it:
// Add constraints to fit to superview, below the nav bar
UIView * hintView = self.refreshBackgroundView;
[hintView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.tableView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-0-[hintView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(hintView)]];
id<UILayoutSupport> guide = self.topLayoutGuide;
[self.tableView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[guide]-0-[hintView]-0-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(hintView, guide)]];
UIRefreshControl
You should really use a UIRefreshControl
for long term compatibility.
Add Vertical Spacing programmatically in swift
Let suppose your UIImageView
is added in the top as you put in your image above, then you can add constraints programmatically like in the following way:
override func viewDidLoad() {
super.viewDidLoad()
// assuming here you have added the self.imageView to the main view and it was declared before.
self.imageView.setTranslatesAutoresizingMaskIntoConstraints(false)
// create the constraints with the constant value you want.
var verticalSpace = NSLayoutConstraint(item: self.imageView, attribute: .Bottom, relatedBy: .Equal, toItem: self.button, attribute: .Bottom, multiplier: 1, constant: 50)
// activate the constraints
NSLayoutConstraint.activateConstraints([verticalSpace])
}
In the above code I only put the vertical space constraints, you need to set the necessary constraints to avoid warnings about it.
There are several ways of adding constraint programmatically you can read more in this very nice answer SWIFT | Adding constraints programmatically.
I hope this help you.
Related Topics
Module Was Not Compiled for Testing' When Using @Testable
Watchkit Appicon - the App Icon Set Named "Appicon" Did Not Have Any Applicable Content
How to Lock Portrait Orientation for Only Main View Using Swift
Change Color of Png in Buttons - iOS
Refresh Certain Row of Uitableview Based on Int in Swift
Using Convertpoint to Get the Relative Position Inside a Parent Uiview
Logging Method Signature Using Swift
Align Text Using Drawinrect:Withattributes:
How to Use Autocorrection and Shortcut List in iOS8 Custom Keyboard
iOS Avplayer Trigger Streaming Is Out of Buffer
What Happens with Constraints When a View Is Removed
Avaudiosession Setcategory Swift 4.2 iOS 12 - Play Sound on Silent
How to Add Done Button on Keyboard on Top of Keyboard in iOS
Xcode 9.2 Upload to App Store Fails with Description Length and Invalid Toolchain Errors
"File Not Found", "Linker Command Failed with Exit Code 1" in Xcode 4.5.1
Present a View Controller, Dismiss It and Present a Different One in Swift
Disable the Interactive Dismissal of Presented View Controller