How to programmatically increase the height of UIView with Swift
You need to create an outlet for the height constraint of the detail view, and then you can adjust the height programmatically like this myHeightConstraint.constant += extraHeight
where extraHeight
is a number indicating how much taller you want the detail view to be. You also may need to call self.view.layoutIfNeeded()
afterwards.
To create an outlet from a constraint, control drag from the constraint in the storyboard editor (just like you would for an outlet of a UIView) into your code.
You are right - because you are using auto layout and constraints, the adjustments need to be made with constraints too. Setting the raw frames can lead to unexpected behavior. Let me know if you have any other questions or difficulties.
How to resize the view in swift programmatically?
First you should connect IBOutlet
of the height constraint from Interface Builder
and then you can change the constant
of the height constraint like this ->
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
// You can change the value of the constraint
searchesShadowHeightConstraint.constant = 115
var imageView = UIImageView()
var image = UIImage(named: "work25")
imageView.image = image
dropoffLocationField.rightView = imageView
dropoffLocationField.rightViewMode = .always
} else {
showToast(with: "Select pick up location")
}
// Then you should update the layout
view.layoutIfNeeded()
}
Set view width size programmatically base on it's subview
I set my customView
width size base on it's sub label like this
customView.frame.size.width = customView.messageLabelWidth + 50 // 50 here is for imageView width and some spacing.
where messageLabelWidth
in FlyingMessageDisplayView
var messageLabelWidth: CGFloat {
get {
return messageLabel.intrinsicContentSize.width
}
}
It will calculate the width of label first base on text length, then update width of customView
Swift make percentage of width programmatically
Maybe because you're doing this before the cell layouts its subviews and it's size is the same as size set in Any-Any size class 600 * 600.
Try doing adding your buttons in layoutSubviews
method, like this:
override func layoutSubviews() {
super.layoutSubviews()
self.layoutIfNeeded()
// add your buttons here
}
Related Topics
Opening Viewcontroller in Appdelegate While Keeping Tabbar
Can't Change Uinavigationbar Prompt Color
Didoutputsamplebuffer Delegate Not Called
Change Duration (Speed) on a Running Animation
Change Splash Screen Image Programmatically
Cocoa Pods Install on iOS Project Not Working
Scaling Physics Bodies in Xcode Spritekit
How to Make Async/Await in Swift
Longer Subtitles in Mapview Annotations (Swift)
Exception: Cannot Manually Set the Delegate on a Uinavigationbar Managed by a Controller
Uitextfield Starting Cursor Position Is Wrong
Running an iOS7 Project Under Xcode 6 - Image Assets Don't Show Up
Replace C Style For-Loop in Swift 2.2.1
Xcode 6 Ignoring Breakpoints - Swift
Why Do I Get an Mkerrordomain Error When Doing a Local Search