I am trying to build a custom-looking action sheet. I thought the easiest way would be creating a view as a subview and assign constraint of subview's top to superview's bottom. And at the same time assigning a cover view with some opacity. Thus, I could have different versions of subview and I can initialise the necessary one and slide it.
I couldn't find anything useful for Swift, so, using this obj-c answer, I tried to convert it to Swift. I achieved the opaque background with this however translating constraints doesn't seem to work.
var coverView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
coverView.backgroundColor = UIColor(white: 0.0, alpha: 0.4)
coverView.alpha = 1.0
self.view.addSubview(coverView)
self.view.bringSubviewToFront(coverView)
}
//doesn't work
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[coverView]|", options: kNilOptions, metrics: nil, views: NSDictionaryOfVariableBindings(coverView)))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[coverView]|", options: kNilOptions, metrics: nil, views: NSDictionaryOfVariableBindings(coverView)))
I got confused on instantiating the view and applying transition animation. If I choose to create a UIView under ViewController, I cannot adjust constraints to adjust equal width of subview to superview.
How can I use the UIView that I created as a Subview (in Storyboard) and then adjust its width constraints so the UI doesn't bug? Also, how can I apply the transition animation so it seems natural?
This link should be here...
I suggested you use UIView xib file and design your view then load in your view controller.
Ex:
Step 1:
Create xib for view
Step 2:
Set background color black for this view, opacity 62% and Alpha = 1
Step 3:
Take new simple UIView and Design your actual view and set constraint.
For Exp:
In your case set view in bottom.
Step 4:
Load xib in view controller.
class calendarViewController: UIViewController
{
var popUpView: popUpView!
override func viewDidLoad() {
super.viewDidLoad()
bookingConfirmView = NSBundle.mainBundle().loadNibNamed("popUpView", owner: self, options: nil).first as! popUpView
// Set Delegate for xib textField
self.popUpView.Name.delegate = self
self.popUpView.MobileNo.delegate = self
}
}
Step 5:
Add this line to where you want to populate view.
self.view.addSubview(bookingConfirmView)
self.bookingConfirmView.frame = self.view.bounds
Related
I have three child VCs that are added to a parent VC. In one of my child VC, I have a view being loaded from a nib. In that nib, I set up a UITextView with no constraints and disabled scrolling so that it dynamically sizes depending on the text. This all works fine so far, I can enter or remove text from the UITextView and it resizes accordingly.
However, if I switch tabs (child VCs), and return back to the one with my UITextView, my UITextView is now of height 0 (cannot be seen). I'm not sure what is causing this to happen, everything works fine until I switch views and return.
My first thought would be to reconfigure my UITextView on viewDidAppear() when returning to my child VC, except my UITextView outlet and setup method is in a separate UIView subclass so I cannot call viewDidAppear(). I'm not even sure if that would be a fix, just what I would guess.
MyParent.swift: UIViewController
MyChild1.swift: UIViewController
MyChild2.swift: UIViewController
MyChild3.swift: UIViewController
MyView.swift: UIView - Custom View loaded from Nib
(this is where my UITextView outlet is and setup for it)
In MyChild3 for example I create the custom view with:
let view = Bundle.main.loadNibNamed("MyView", owner: self, options: nil)?.first! as! MyView
view.myModel = model
view.configure()
Then, in MyView.swift which is the custom class that the nib uses, I have my UITextView outlet and setup method:
#IBOutlet weak var textView: UITextView!
public func configure() {
configureTextView()
}
private func configureTextView() {
textView.delegate = self
textView.text = myModel.text
textView.isScrollEnabled = false
}
EDIT:
I should mention that my UITextView is a part of a horizontal StackView along with a UIImageView, as follows:
UIImageView
UITextView
When I return to my VC, the UIImageView takes up 100% of the height in the StackView, although before leaving my VC, the UITextView height was sized correctly to its text. After doing Show View Hierarchy, I cannot even find my UITextView anymore, only the UIImageView seems to be in the StackView. I essentially want my UITextView to take up as much height as it needs in the StackView, and the UIImageView take up the rest, but the UIImageView is taking it all up.
I did manage to find this error?
I would try setting content compression resistance of the textView to a higher value, maybe that's what causing ambiguity:
textView.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 999), for: .vertical)
I have an iMessage extension and I'm having some issues with changing presentation styles. When I first open the app here is what I get:
That's how it should be. Now when I change to expanded presentation style, this is what I get:
That's also what I want. However, when I switch back to compact, this happens:
Here is my code:
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
super.didTransition(to: presentationStyle)
presentSearchStickersView()
}
private func presentSearchStickersView() {
let controller = (storyboard?.instantiateViewController(withIdentifier: "SearchStickersViewController"))! as! SearchStickersViewController
controller.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
controller.searchDelegate = self
for child in childViewControllers {
child.willMove(toParentViewController: nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}
self.addChildViewController(controller)
self.view.addSubview(controller.view)
}
And here is a screenshot of my top constraint:
In my point of view you should not reinstantiate the bar every time you switch to compact or extend mode. You should instantiate it once, then set constraints to the top of the view. I've tried that way and it's working fine ;)
So to sum up, if you are using the storyboard
In your storyboard add your subview to the controller
Set a Top constraint, width equal to superview and centerX to superview
In the code set your search bar (delegate etc) in viewdidload
If you are not using storyboard.
load your xib and add it to your subview (maybe in the didBecomeActive or something like that)
Don't forget to set the translatesAutoresizingMaskIntoConstraints to false
Add the same constraints as above
As RomOne said you should be putting it there once. Style switches should be handled by constraints
I have come across a conundrum of sorts in regards unexpected (at least for me) sizes of UIViews.
Consider this UIViewController class. interfaceBuilderView was declared in a Storyboard file and constrained to take up the whole area of the UIView. So, I would expect to have interfaceBuilderView be the same size as programicallyCreatedView when calling *.frame.width. But they aren't.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var interfaceBuilderView: MyCustomView!
override func viewDidLoad() {
super.viewDidLoad()
let programmicallyCreatedView = MyCustomView(frame: self.view.frame)
//commented out this to get first picture
self.view.addSubview(programmicallyCreatedView)
self.interfaceBuilderView.setAppearance()
self.programmicallyCreatedView.setAppearance()
print(self.view.frame.width)//prints 375
print(self.interfaceBuilderView.frame.width)//prints 600
print(self.programmicallyCreatedView.frame.width)//prints 375
}
}
Now, consider this implementation of the MyCustomView class.
import UIKit
class MyCustomView: UIView {
func setAppearance() {
let testViewWidth: CGFloat = 200.0
let centerXCoor = (self.frame.width - testViewWidth) / 2.0
let testView = UIView(frame: CGRectMake(centerXCoor, 0, testViewWidth, 100))
testView.backgroundColor = UIColor.redColor()
self.addSubview(testView)
}
}
As you can see, I simply draw a red rectagle of width 200.0, and it is supposed to be centered. Here are the results.
Using the Interface Builder created view.
And using the programmatically created view.
As you can see, the programmatically created view achieves the desired results. No doubt because the size printed is the same as the superview (375).
Therefore, my question is simply why is this happening? Furthermore, how can I use a view declared in interface builder and programmatically add other views to it with dimensions and placement that I expect?
A few thoughts:
This code is accessing frame values in viewDidLoad, but the frame values are not yet reliable at that point. The view hasn't been laid out yet. If you're going to mess around with custom frame values, do this in viewDidAppear or viewDidLayoutSubviews.
Nowadays, we really don't generally use frame values anymore. Instead, we define constraints to define the layout programmatically. Unlike custom frame values, you can define constraints when you add the subviews in viewDidLoad.
You have the scene's main view, the MyCustomView and then yet another UIView which is red. That strikes me as unnecessarily confusing.
I would advise that you just add your programmatically created subview in viewDidLoad and specify its constraints. Using the new iOS 9 constraints syntax, you can just specify that it should be centered, adjacent to the top of the view, half the width, and one quarter the height:
override func viewDidLoad() {
super.viewDidLoad()
let redView = UIView()
redView.backgroundColor = UIColor.redColor()
redView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(redView)
NSLayoutConstraint.activateConstraints([
redView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor),
redView.topAnchor.constraintEqualToAnchor(view.topAnchor),
redView.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier: 0.5),
redView.heightAnchor.constraintEqualToAnchor(view.heightAnchor, multiplier: 0.25)
])
}
Clearly, adjust these constraints as suits you, but hopefully this illustrates the idea. Don't use frame anymore, but rather use constraints.
I have UITextView that I would like to make the same height and width of it's container. It is in a simple UIViewContainer.
I tried doing the following:
override public func viewDidLoad() {
self.edgesForExtendedLayout = .None
resultText.text = result
resultText.frame = view.frame
}
This seems to work for portrait but not landscape.
All I am trying to do is make the UITextView take up all the space of it's container.
If I could find the answer in Objective-C I could easily translate it to Swift. I am just looking for the answer for iOS.
I suggest you to use auto layout
Then click add 4 constraints.
If any warning,
Click Update Frames
Autolayout is your friend - It can be done easily using Interface Builder, or in code:
override public func viewDidLoad() {
super.viewDidLoad()
self.edgesForExtendedLayout = .None
resultText.text = result
// add vertical constraints to pin the view to the superview edge
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0.0-[resultText]-0.0-|", options: nil, metrics: nil, views: ["resultText": resultText]))
// add horizontal constrains to pin the view to the superview edge
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0.0-[resultText]-0.0-|", options: nil, metrics: nil, views: ["resultText": resultText]))
}
How do you get the width and height of a UIView who's size and position are set using Auto Layout and Apple's Visual Format Language?
Here's the code (view is just the variable from UIViewController):
// Create and add the view
var stageView = UIView()
stageView.setTranslatesAutoresizingMaskIntoConstraints(false) // Since I'm using Auto Layout I turn this off
view.addSubview(stageView)
// Create and add constraints to the containing view
let viewsDictionary = ["stageView":stageView]
let horizontalConstraints: NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-150-[stageView]-150-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
let verticalConstraints: NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-100-[stageView]-150-|", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: viewsDictionary)
view.addConstraints(horizontalConstraints)
view.addConstraints(verticalConstraints)
println("stageView.frame=\(stageView.frame)")
and got:
stageView.frame=(0.0,0.0,0.0,0.0)
so I tried:
let fittingSize = stageView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
println("fittingSize=\(fittingSize)")
and got:
fittingSize=(0.0,0.0)
I can't seem to find a way to get the size. I'm able to add subviews to stageView that place just fine using Auto Layout and Visual Format Language, but I can't get width and height for stageView which I need to further position those subviews.
Any ideas?
You have a few options:
You can force the layout engine to size the views immediately by calling setNeedsLayout and then call layoutIfNeeded. This is not recommended because it's inefficient and any manual frames required for layout might not have been set yet. You can read more about this approach on my answer to this question.
You can also wait until the subviews have been updated in the view controller:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
println("stageView.frame = \(stageView.frame)")
}
If you want to know within a UIView subclass (or more often, a UITableViewCell subclass, you can check after layoutSubviews has run:
override func layoutSubviews() {
super.layoutSubviews()
println("self.frame = \(self.frame)")
}
You need to check the frame inside viewDidLayoutSubviews.
This function run after constraint calculation
Its suppose to look something like this
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
//Print frame here
}