I'm trying to cope with iPhone X with inputAccessoryView. I've added a view to my ViewController like so:
CustomView
I've defined an outlet and attached to it. Returned the same view as inputAccessoryView like so:
class ViewController: UIViewController {
#IBOutlet weak var textFieldContainer: UIView!
override var inputAccessoryView: UIView? {
return textFieldContainer
}
override var canBecomeFirstResponder: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textFieldContainer.autoresizesSubviews = true
}
}
I've made sure to add constraints relative to safe area. Here are my constraints:
Constraints
Adjusted autoResizingMask in SB like so:
AutoResizingMask
However, it's still not working. Here's the output:
Output
What did I miss?
Unlike for table view cells, there is no support for dynamic height calculation in input accessory views, as far as I know.
You could use a fixed height for the accessory view.
But I assume, that you want just to change either top, bottom, or height constraint in interface builder and the change is reflected after the next build.
What you could do is, to use a custom view class where you connect your top, bottom and height constraints.
Then override intrinsicContentSize and return the sum of the three constraint constants.
class TextFieldContainer: UIView {
#IBOutlet weak var topConstraint: NSLayoutConstraint!
#IBOutlet weak var bottomConstraint: NSLayoutConstraint!
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
override var intrinsicContentSize: CGSize {
let contentHeight =
self.topConstraint.constant
+ self.heightConstraint.constant
+ self.bottomConstraint.constant
return CGSize(width: UIScreen.main.bounds.width, height: contentHeight)
}
}
Your layout hierarchy could be simplified and look like this:
The autoresizing mask could look like this:
The final result would behave like the following then:
You're giving textfield height and also bottom constraint, try to remove bottom constraint for textfield
Related
I want to allow users to scroll left or right but only one screen far.
//
// MainViewController.swift
// Calendar
//
// Created by Andy on 7/22/17.
// Copyright © 2017 Andy. All rights reserved.
//
import UIKit
class MainViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var ScrollView: UIScrollView!{
didSet{
ScrollView.delegate = self
ScrollView.contentSize = CGSize(width: ScrollView.frame.width * 3, height: ScrollView.frame.height)
}
}
#IBOutlet weak var center: UIView!
#IBOutlet weak var left: UIView!
#IBOutlet weak var right: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ReusableViewController {
if let identifier = segue.identifier{
destination.currentYearOffSet = 0
destination.currentMonthOffSet = 0
switch identifier {
case "center":
destination.position = .center
case "left":
destination.position = .left
case "right":
destination.position = .right
default:
fatalError("Unexpected Segue Identifier")
}
}
}
}
}
This Controller does nothing more than getting the scroll work. And inside the scrollview there are three containerViews managed by other controllers. But that simply does not work.
I have done the following to solve the problem but in vain: 1. Check the content size of scrollView. 2. Hook up the controller and the three containerViews. 3. Set the scrollView delegate but I haven't implemented any method.(Maybe that's the cause but I don't know what to implement)
Plus, do I need to hook up those three containerViews when the controller does nothing about them?
EDIT: I removed: 1. contentSize 2. UIScrollViewDelegate 3. outlets of the three containerViews. This does make my code a lot more nicer, but scrollView still cannot scroll.
EDIT2: I add a view which is exactly the size I want scrollView.contentSize to be as the scrollView's immediate subview. And it seem that the content height is solved because it's only saying that the content width is ambiguous. It want to to add leading and trailing constraints to the view I added but I want it to just center horizontally in the scrollView. Now I need to set the leading and trailing constraints to let autoLayout know the contentWidth. How can I set the leading and trailing offSet based on the width of its superview or I'm supposed to do something else to solve this problem?
You do not need to set like this:-
#IBOutlet weak var ScrollView: UIScrollView!{
didSet{
ScrollView.delegate = self
ScrollView.contentSize = CGSize(width: ScrollView.frame.width * 3, height: ScrollView.frame.height)
}
}
Just having outlet #IBOutlet weak var ScrollView: UIScrollView! and pinning proper constraint(leading, trailing, top, bottom, equal Width(should be equal to or greater than to), equal height (should be equal to or greater than to).
For Horizontal scroll
If you wanted to scrolling horizontally then also pinned width constraint, this is required to avoid auto layout error but inside placeholder checked Remove at build time.
For Vertical scroll
If you wanted to scrolling vertically then also pinned height constraint, this is required to avoid auto layout error
but inside placeholder checked Remove at build time.
Note:- You can use either both or any one based on your requirement.
I want to add a few view in scrollview. But I dont want to this view programmatically. I want to create with storyboard. This view include an image. I added scrollview and added to view. But in scrollview I just showing my last view. I want to 2 view and 2 image like my view. How can i fix this?
This is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
#IBOutlet weak var myView: UIView!
#IBOutlet weak var imgv: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageArray = ["image","image2"]
for i in 0..<imageArray.count{
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
myView.layer.frame.size.width = myView.frame.width * CGFloat(i + 1)
imgv.image = imageArray[i]
mainScrollView.addSubview(myView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
This is storyboard and controller looks like
You are having an issue with constraints from what I see and that is why you might not see every element that you put on your storyboard. You need to correct those errors for everything to show up correctly in your scrollview. Keep in mind, you need to tell the scrollview what its height and width should be in the storyboard. They need to be defined in some way. This is a common mistake when working with scrollviews.
You can put a view in your scrollview and define its height and width property. You can set its width to be equal to the screen's width and you can set a constant for your height.
I have a custom UICollectionViewCell that is laid out in an .xib file. The size is 194 x 200 AND auto layout constraints are set:
and this is what the class looks like
class DurationDayCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var banner: UIView!
#IBOutlet weak var numberOfHours: UILabel!
#IBOutlet weak var unitHrs: UILabel!
#IBOutlet weak var dayOfWeek: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
But in my collection view it looks like this:
The size of the cells are smaller (97 x 100). IS there a way to make the nib to auto-adjust to the size of the collection view cell?
Had the exact same problem, solved with either of those two solutions:
in your storyboard, click Collection View, click size inspector, Set "Estimate Size" to "None"
or
in your custom cell class, add this method:
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
let preferredLayoutAttributes = layoutAttributes
var adjustedFrame = layoutAttributes.frame
adjustedFrame.size.height = 180 //change according to your case
adjustedFrame.size.width = 100
preferredLayoutAttributes.frame = adjustedFrame
return preferredLayoutAttributes
}
Click your xib on stotyboard and find right side Simulator Metrics and change size to -> Freeform later click Show size inspector select freeform and write there your width and height later build will works !
Thanks
I'm trying to change the height and width of UIView using Swift and it's not working. Tried both codes below, still no progress.
self.contentView.frame.size.height = 100
self.contentView.bounds.size.height = 100
contentView is a subview of the main view.
I guess you are using constraints. And when you use constraints you should change not the frame but constraints itself. It should looks like this:
In storyboard, pick height constraint for your view and create outlet to controller:
And then change it's constant value:
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
func changeHeight() {
heightConstraint.constant = 200
// uncomment to perform changes with animation
// UIView.animateWithDuration(0.3) { () -> Void in
self.view.layoutIfNeeded()
// }
}
I am new to swift and am trying to add a UIScrollview to a custom UITableViewCell. I can't seem to get the scrollview to scroll in the simulator though. I have tested both vertical and horizontal scrolling.
Here is the code for my custom UITableViewCell
import UIKit
class CustomTableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var winLoseValueLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var newTotalLabel: UILabel!
#IBOutlet weak var locationLabel: UILabel!
#IBOutlet weak var playersLabel: UILabel!
#IBOutlet weak var playersScrollView: UIScrollView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.playersLabel.text = nil
//self.playersScrollView.contentSize.height = 1000
self.playersScrollView.contentSize.width = 1000
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func addLabelToScrollView(str : String) {
if self.playersLabel.text == nil{
self.playersLabel.text = str
}
else{
self.playersLabel?.text = self.playersLabel.text! + "\n\(str)"
}
}
}
My playersScrollView outlet has been properly connected to the scrollview in my storyboard. As far as I can see from all of the tutorials and sources online, I should just have to set the contentSize width or height (larger than the view area) to get it to scroll, which I have done in the awakeFromNib function.
Any ideas why this is not working in the simulator? Secondly, ideally I would like to have a vertical scrolling scrollview in the table cell, but I thought that the table view's vertical scrolling might have been interfering with the scrollview, so I changed it for horizontal scrolling for testing, which also didn't work.
Can you have a vertical scrollview in a vertically scrolling UITableView, or will the table view scrolling interfere too much with the scrollview?
Ok, it turns out that the constraints on my playersLabel, which is a subview of playersScrollView, was the culprit, although I am not sure why. I adjusted the constraints on the label and that allowed the horizontal scrolling to work.
The vertical scrolling still did not work, so I had to override the touchesBegan and touchesEnded methods in the scroll view so I could disable/re-enable the scrolling of the table view.