inputAccessoryViewController height modification - ios

I'm trying to use inputAccessoryViewController in my app, but faced a problem with changing height of accessory view. I tried to change frame/bounds of the view, I also tried to handle height of the accessory view using constraints. But nothing worked well.
InputViewController code:
import UIKit
import RxSwift
import RxCocoa
class InputViewController: UIInputViewController {
private var separatorView: UIView?
private var answerTextView: ConstrainedTextView?
private var closeButton: UIButton?
private var tipLabel: UILabel?
// private var generalHeightConstraint: NSLayoutConstraint?
private var separatorHeightConstraint: NSLayoutConstraint?
private var answerTextViewBottomConstraint: NSLayoutConstraint?
private let junk = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
private func configureView() {
// view.autoresizingMask = .flexibleHeight
view.backgroundColor = UIColor.white
view.frame = CGRect(x: 0, y: 0, width: view.superview?.bounds.width ?? 100, height: 70)
// view.translatesAutoresizingMaskIntoConstraints = false
// generalHeightConstraint = AutoLayoutSetAttribute(view, .height, 70)
// Separator
separatorView = UIView()
separatorView?.backgroundColor = UIColor.gray
separatorView?.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(separatorView!)
AutoLayoutEqualizeSuper(separatorView, .left, 0)
AutoLayoutEqualizeSuper(separatorView, .right, 0)
AutoLayoutEqualizeSuper(separatorView, .top, 0)
separatorHeightConstraint = AutoLayoutSetAttribute(separatorView, .height, 1)
// Close Button
closeButton = UIButton(type: .system)
closeButton?.setTitle("Hide", for: .normal)
closeButton?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
closeButton?.translatesAutoresizingMaskIntoConstraints = false
closeButton?.addTarget(self, action: #selector(dismissKeyboard), for: .touchUpInside)
view.addSubview(closeButton!)
AutoLayoutSetAttribute(closeButton, .width, 70)
AutoLayoutEqualizeSuper(closeButton, .right, -5)
view.addConstraint(NSLayoutConstraint(item: closeButton!, attribute: .top, relatedBy: .equal, toItem: separatorView, attribute: .bottom, multiplier: 1, constant: 0))
// Tip Label
tipLabel = UILabel()
tipLabel?.textColor = UIColor.darkGray
tipLabel?.text = "Input answer:"
tipLabel?.font = UIFont.systemFont(ofSize: 17)
tipLabel?.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tipLabel!)
AutoLayoutEqualizeSuper(tipLabel, .left, 5)
AutoLayoutEqualize(tipLabel, separatorView, .top, 0)
view.addConstraint(NSLayoutConstraint(item: tipLabel!, attribute: .right, relatedBy: .equal, toItem: closeButton, attribute: .left, multiplier: 1, constant: 0))
// Text View
answerTextView = ConstrainedTextView()
answerTextView?.backgroundColor = UIColor.white
answerTextView?.delegate = self
answerTextView?.scrollsToTop = false
answerTextView?.showsVerticalScrollIndicator = false
answerTextView?.font = REG_FONT(15)
answerTextView?.maxLines = 5
answerTextView?.translatesAutoresizingMaskIntoConstraints = false
answerTextView?.layer.masksToBounds = true
answerTextView?.layer.cornerRadius = 7
answerTextView?.layer.borderColor = UIColor.lightGray.withAlphaComponent(0.7).cgColor
answerTextView?.layer.borderWidth = 1
view.addSubview(answerTextView!)
AutoLayoutEqualizeSuper(answerTextView, .left, 5)
AutoLayoutEqualizeSuper(answerTextView, .right, -5)
answerTextViewBottomConstraint = AutoLayoutEqualizeSuper(answerTextView, .bottom, -5)
view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: closeButton, attribute: .bottom, multiplier: 1, constant: 0))
answerTextView?
.rx
.observe(CGRect.self, "bounds")
.distinctUntilChanged {
$0?.size.height == $1?.size.height
}
.subscribe { [unowned self] newBounds in
if var newHeight = newBounds.element??.size.height,
let separatorHeight = self.separatorHeightConstraint?.constant,
let buttonHeight = self.closeButton?.intrinsicContentSize.height,
let bottomSpace = self.answerTextViewBottomConstraint?.constant {
newHeight = newHeight == 0 ? 30 : newHeight
let generalHeight = newHeight + separatorHeight + buttonHeight + abs(bottomSpace)
self.view.frame = CGRect(x: 0, y: 0, width: self.view.superview?.bounds.width ?? 100, height: generalHeight)
// self.generalHeightConstraint?.constant = generalHeight
// UIView.animate(withDuration: 0.2) {
// self.view.setNeedsLayout()
// self.view.layoutIfNeeded()
// }
}
}
.addDisposableTo(junk)
}
}
// MARK: - UITextViewDelegate Protocol Conformance
extension InputViewController: UITextViewDelegate {
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
textView.inputAccessoryView = view
return true
}
func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
textView.inputAccessoryView = nil
return true
}
}
View Controller where input accessory VC is used:
import UIKit
class TestViewController: UIViewController {
override var inputAccessoryViewController: UIInputViewController? {
return SDAnswerInputViewController()
}
override var canBecomeFirstResponder: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Can you explain how shall I correctly modify height of input accessory view overriding inputAccessoryViewController?

The problem was in these two lines:
view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: answerTextView!, attribute: .top, relatedBy: .equal, toItem: closeButton, attribute: .bottom, multiplier: 1, constant: 0))
The answerTextView couldn't modify it's height because of constraints at the bottom and the top.

Related

Swift: Stacking subviews dynamically inside a superview

I am trying to create a login in page that is supposed to look like the one listed below. The code I have written should produce this view minus the logo and minus [Login|Register] toggle button. The height and width of my box is also different, but I'm not concerned about that.
Currently I am getting this output. I'm concerned about how the words are all overlapping each other at the top.
In the code I listed below I create my 3 text fields, my button and the container for my text fields. I believe there is something wrong in the function fieldConstraints. In this function I look through an array of all my text fields and assign the neccessary constraints to them. They all get the same constraints except that the topAnchor of each text field after the first one is set equal to the bottomAnchor of the separator that was placed underneath the text field before. Those blue lines in between the text fields are the separators.
Main class
class SignIn: UIViewController {
override func loadView() {
super.loadView()
let inputContainer = inputDataContainer()
constraintsToCenterSubview(forView: inputContainer, width: 100, height: 100)
let nameField = field(for: "Name")
let emailField = field(for: "Email address")
let passField = field(for: "Password")
let fields = [nameField, emailField, passField]
let button = loginButton()
fieldConstraints(subviews: fields, superview: inputContainer)
self.centerViewBelow(forView: button, whichIsBelow: inputContainer, increaseWidthBy: 0)
}
func inputDataContainer() -> UIView{
let inputView = UIView(frame: CGRect(x: self.view.center.x, y: self.view.center.y, width: CGFloat(100), height: CGFloat(100)))
inputView.backgroundColor = UIColor.white
inputView.translatesAutoresizingMaskIntoConstraints = false
inputView.layer.cornerRadius = 5
inputView.layer.masksToBounds = true
self.view.addSubview(inputView)
//inputView = centerViewBelow(forView: inputView, whichIsBelow: self.view, increaseWidthBy: 100)
return inputView
}
func loginButton() -> UIButton {
let button = UIButton()
button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
button.setTitle("Submit", for: [])
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(UIColor.white, for: [])
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
self.view.addSubview(button)
return button
}
func field(for name: String) -> UITextField{
let tf = UITextField()
tf.placeholder = name
tf.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(tf)
return tf
}
func fieldSep() -> UIView {
let view = UIView()
view.backgroundColor = UIColor(r: 220, g: 220, b: 220)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
func fieldConstraints(subviews: [UIView], superview: UIView) {
var sep: UIView?
let len = subviews.endIndex
for (idx, subview) in subviews.enumerated(){
superview.addSubview(subview)
subview.leftAnchor.constraint(equalTo: superview.leftAnchor)
subview.rightAnchor.constraint(equalTo: superview.rightAnchor)
subview.widthAnchor.constraint(equalTo: superview.widthAnchor)
subview.heightAnchor.constraint(equalTo: superview.heightAnchor, multiplier: CGFloat(1/len))
if (sep != nil){
subview.topAnchor.constraint(equalTo: sep!.bottomAnchor)
}else{
subview.topAnchor.constraint(equalTo: superview.topAnchor)
}
sep = fieldSep()
if idx < subviews.endIndex-1 {
self.view.addSubview(sep!)
sep?.leftAnchor.constraint(equalTo: superview.leftAnchor)
sep?.rightAnchor.constraint(equalTo: superview.rightAnchor)
sep?.topAnchor.constraint(equalTo: subview.bottomAnchor)
}
}
}
}
Extensions
extension UIColor {
convenience init(r:CGFloat, g:CGFloat, b: CGFloat) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: 1)
}
}
extension UIViewController {
func centerViewBelow(forView view: UIView, whichIsBelow topView: UIView, increaseWidthBy constant: CGFloat){
let topConstraint = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: topView, attribute: .bottom, multiplier: 1, constant: 20)
let widthConstraint = NSLayoutConstraint(item: view, attribute: .width, relatedBy: .equal, toItem: topView, attribute: .width, multiplier: 1, constant: constant)
let centerConstraint = NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
let heightConstraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 50)
NSLayoutConstraint.activate([topConstraint, widthConstraint, centerConstraint, heightConstraint])
//return view
}
func constraintsToCenterSubview(forView view: UIView, width: Int, height: Int){
let centerXConstraint = NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
let centerYConstraint = NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0)
let widthConstraint = NSLayoutConstraint(item: view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: CGFloat(width))
let heightConstraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: CGFloat(height))
NSLayoutConstraint.activate([centerXConstraint, centerYConstraint, widthConstraint, heightConstraint])
}
}
Thank you
----------------------------------------------------------------------------------------------------------
Update
So I was pretty much able to do it by changing my container into a stack view. But this made it so that my corners were no longer rounded. Does anyone know how to fix this?
func inputDataContainer() -> UIStackView{
let inputView = UIStackView(frame: CGRect(x: self.view.center.x, y: self.view.center.y, width: CGFloat(100), height: CGFloat(100)))
inputView.backgroundColor = UIColor.white
inputView.translatesAutoresizingMaskIntoConstraints = false
inputView.layer.cornerRadius = 5
inputView.layer.masksToBounds = true
inputView.distribution = .fillEqually
inputView.axis = .vertical
inputView.spacing = 1
self.view.addSubview(inputView)
//inputView = centerViewBelow(forView: inputView, whichIsBelow: self.view, increaseWidthBy: 100)
return inputView
}
func fieldConstraints(subviews: [UIView], superview: UIStackView) {
for subview in subviews{
superview.addArrangedSubview(subview)
subview.clipsToBounds = true
}
}
Example Screen Shot of Current App
Try to give height for
sep?.heightAnchor.constraint(equalToConstant:1.0).isActive = true
also for all constraints in fieldConstraints you forget
.isActive = true
or use NSLayoutConstraint.activate , like
NSLayoutConstraint.activate([
subview.leftAnchor.constraint(equalTo: superview.leftAnchor),
subview.rightAnchor.constraint(equalTo: superview.rightAnchor)
subview.widthAnchor.constraint(equalTo: superview.widthAnchor),
subview.heightAnchor.constraint(equalTo: superview.heightAnchor, multiplier: CGFloat(1/len))
])
This approach will work but it will be better to use a vertical stackview with distribution .fillEqually and it'll will partition them and add like
fileds.forEach { stackview.addArrangedSubview($0) }
use UIStackView. it will help you save time to build a complicated UI.
You will have a little trouble using a UIStackView. You could embed it in a UIView and give that view the rounded corners, but then you have to add padding to the fields within the stack view in order to get your separator lines.
Here is another approach that might work better for you. It's closer to your original code, adding the fields and separator views to a UIView:
extension UIColor {
convenience init(r:CGFloat, g:CGFloat, b: CGFloat) {
self.init(red: r/255, green: g/255, blue: b/255, alpha: 1)
}
}
class SampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(r: 65, g: 92, b: 144)
let nameField = field(for: "Name")
let sep1 = fieldSep()
let emailField = field(for: "Email address")
let sep2 = fieldSep()
let passField = field(for: "Password")
let views = [nameField, sep1, emailField, sep2, passField]
let inputView = inputDataContainer(with: views)
view.addSubview(inputView)
NSLayoutConstraint.activate([
inputView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0),
inputView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0),
inputView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0.0),
])
}
func field(for s: String) -> UITextField {
let f = UITextField()
f.translatesAutoresizingMaskIntoConstraints = false
f.placeholder = s
f.borderStyle = .none
return f
}
func fieldSep() -> UIView {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.heightAnchor.constraint(equalToConstant: 1.0).isActive = true
v.backgroundColor = UIColor(r: 220, g: 220, b: 220)
return v
}
func inputDataContainer(with subviews: [UIView]) -> UIView {
let horizontalPadding: CGFloat = 8.0
let verticalSpacing: CGFloat = 8.0
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = .white
containerView.layer.cornerRadius = 5
var previousView: UIView?
for subview in subviews{
containerView.addSubview(subview)
// if it's a text field, we want padding on left and right
if subview is UITextField {
subview.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: horizontalPadding).isActive = true
subview.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -horizontalPadding).isActive = true
} else {
subview.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0.0).isActive = true
subview.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0.0).isActive = true
}
if subview == subviews.first {
// if it's the first subview, constrain to top of container
subview.topAnchor.constraint(equalTo: containerView.topAnchor, constant: verticalSpacing).isActive = true
} else {
// unwrap previousView and constrain subview to bottom of previous subview
if let pv = previousView {
subview.topAnchor.constraint(equalTo: pv.bottomAnchor, constant: verticalSpacing).isActive = true
}
}
if subview == subviews.last {
// if it's the last subview, constrain to bottom of container
subview.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -verticalSpacing).isActive = true
}
// save reference to current subview
previousView = subview
}
return containerView
}
}
And the result:

Programmatically create UIView with size based on subviews(labels)

I have a scenario where I'm needed to create an UIView completely programatically. This view is displayed as an overlay over a video feed and it contains 2 UILabels as subviews.
These labels can have varying widths and heights so I don't want to create a fixed frame for the UIView or the labels.
I've done this with programatic constrains without issue, but the problem is that the views are glitching when moving the phone and the framework that I'm using that does the video feed recommends not to use autolayout because of this exact problem.
My current code:
I create the view
func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView {
let annotationView = AnnotationView() //*this is the view, inherits from UIView
annotationView.annotation = viewForAnnotation
annotationView.delegate = self
return annotationView
}
And the actual view code
class AnnotationView: ARAnnotationView {
var titleLabel: UILabel?
var distanceLabel: UILabel!
var delegate: AnnotationViewDelegate?
override var annotation: ARAnnotation? {
didSet {
loadUI()
}
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
setupUI()
}
private func setupUI() {
layer.cornerRadius = 5
layer.masksToBounds = true
backgroundColor = .transparentWhite
}
private func loadUI() {
titleLabel?.removeFromSuperview()
distanceLabel?.removeFromSuperview()
let label = UILabel()
label.font = UIFont(name: "AvenirNext-Medium", size: 16.0) ?? UIFont.systemFont(ofSize: 14)
label.numberOfLines = 2
label.textColor = UIColor.white
self.titleLabel = label
distanceLabel = UILabel()
distanceLabel.textColor = .cbPPGreen
distanceLabel.font = UIFont(name: "AvenirNext-Medium", size: 14.0) ?? UIFont.systemFont(ofSize: 12)
distanceLabel.textAlignment = .center
self.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
distanceLabel.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(label)
self.addSubview(distanceLabel)
if self.constraints.isEmpty {
NSLayoutConstraint.activate([
NSLayoutConstraint(item: label, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 5),
NSLayoutConstraint(item: label, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: -5),
NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: distanceLabel, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: distanceLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0),
NSLayoutConstraint(item: distanceLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0),
NSLayoutConstraint(item: distanceLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0),
NSLayoutConstraint(item: distanceLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 15),
])
}
if let annotation = annotation as? BikeStationAR {
titleLabel?.text = annotation.address
distanceLabel?.text = String(format: "%.2f km", annotation.distanceFromUser / 1000)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
delegate?.didTouch(annotationView: self)
}
}
So my problem is now: how can I achieve the same behavior, without using autolayout?
Edit: here is a video of the behavior https://streamable.com/s/4zusq/dvbgnb
Edit 2: The solution is based on Mahgol Fa's answer and using a stack view or vertical positioning in order not to set the frame for the individual labels. So the final implementation is
private func loadUI() {
titleLabel?.removeFromSuperview()
distanceLabel?.removeFromSuperview()
let label = UILabel()
label.font = titleViewFont
label.numberOfLines = 0
label.textColor = UIColor.white
label.textAlignment = .center
self.titleLabel = label
distanceLabel = UILabel()
distanceLabel.textColor = .cbPPGreen
distanceLabel.font = subtitleViewFont
distanceLabel.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
distanceLabel.translatesAutoresizingMaskIntoConstraints = false
if let annotation = annotation as? BikeStationAR {
let title = annotation.address
let subtitle = String(format: "%.2f km", annotation.distanceFromUser / 1000)
let fontAttributeTitle = [NSAttributedString.Key.font: titleViewFont]
let titleSize = title.size(withAttributes: fontAttributeTitle)
let fontAttributeSubtitle = [NSAttributedString.Key.font: subtitleViewFont]
let subtitleSize = annotation.address.size(withAttributes: fontAttributeSubtitle)
titleLabel?.text = title
distanceLabel?.text = subtitle
let stackView = UIStackView(frame: CGRect(x: 0, y: 0, width: titleSize.width + 5, height: titleSize.height + subtitleSize.height + 5))
stackView.axis = .vertical
stackView.distribution = .fillProportionally
stackView.alignment = .fill
stackView.addArrangedSubview(titleLabel ?? UIView())
stackView.addArrangedSubview(distanceLabel)
frame = stackView.bounds
addSubview(stackView)
}
}
This way you can get the width of your lablels texts:
let fontAttributes1= [NSAttributedStringKey.font: your declared font in your code]
let size1 = (“your string” as String).size(withAttributes: fontAttributes1)
let fontAttributes2= [NSAttributedStringKey.font: your declared font in your code]
let size2 = (“your string” as String).size(withAttributes: fontAttributes2)
Then :
YourView.frame = CGRect( width: size1.width + size2.width + some spacing , height : ....)

Customize UISearchBar in Swift 4

I need to delete the background of the search bar.
The part where text is entered (white color) make it higher.
put a green border around the white part.
customize the font.
I need this:
What I have achieved is this:
My code:
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar.layer.borderColor = #colorLiteral(red: 0.2352941176, green: 0.7254901961, blue: 0.3921568627, alpha: 1)
self.searchBar.layer.borderWidth = 1
self.searchBar.clipsToBounds = true
self.searchBar.layer.cornerRadius = 20
}
Try using this code:
class JSSearchView : UIView {
var searchBar : UISearchBar!
override func awakeFromNib()
{
// the actual search barw
self.searchBar = UISearchBar(frame: self.frame)
self.searchBar.clipsToBounds = true
// the smaller the number in relation to the view, the more subtle
// the rounding -- https://www.hackingwithswift.com/example-code/calayer/how-to-round-the-corners-of-a-uiview
self.searchBar.layer.cornerRadius = 5
self.addSubview(self.searchBar)
self.searchBar.translatesAutoresizingMaskIntoConstraints = false
let leadingConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 20)
let trailingConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: -20)
let yConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
self.addConstraints([yConstraint, leadingConstraint, trailingConstraint])
self.searchBar.backgroundColor = UIColor.clear
self.searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
self.searchBar.tintColor = UIColor.clear
self.searchBar.isTranslucent = true
// https://stackoverflow.com/questions/21191801/how-to-add-a-1-pixel-gray-border-around-a-uisearchbar-textfield/21192270
for s in self.searchBar.subviews[0].subviews {
if s is UITextField {
s.layer.borderWidth = 1.0
s.layer.cornerRadius = 10
s.layer.borderColor = UIColor.green.cgColor
}
}
}
override func draw(_ rect: CGRect) {
super.draw(rect)
// the half height green background you wanted...
let topRect = CGRect(origin: .zero, size: CGSize(width: self.frame.size.width, height: (self.frame.height / 2)))
UIColor.green.set()
UIRectFill(topRect)
}
}
And to use it, drop a custom view into your xib or storyboard file and simply set the custom class type to the name of the class (JSSearchView).

Two UISlider and UILabel setting them programmatically but does not know which slider is slides

I have an iOS project in which I am setting up two sliders and corresponding labels.
Labels are for the slider value. When they slide, the value in the labels change.
I have another class in which I set the slider and label after they create and save that instance in the array. The difficulty I face is when I slide the slider, I do not know which slider is slides.
Here is code:
SliderWidget Class
class SliderWidget {
private var label: UILabel?
private var slider: UISlider?
public func setLable(label: UILabel) {
self.label = label
}
public func setSlider(slider: UISlider) {
self.slider = slider
}
public func getLabel() -> UILabel {
return self.label!
}
public func getSlider() -> UISlider {
return slider!
}
}
Here is the controller on which I set slider and label
class WidgetsVC: UIViewController {
private let step: Float = 10
private var labels: UILabel?
private var sliderArray: Array<SliderWidget> = Array()
override func viewDidLoad() {
super.viewDidLoad()
displaySlider(sliderY: 100,labelY: 70)
displaySlider(sliderY: 200, labelY: 170)
}
private func displaySlider(sliderY: Int, labelY: Int) {
let slider = UISlider()
slider.tintColor = #colorLiteral(red: 0.2980392277, green: 0.6862744689, blue: 0.3137254715, alpha: 1)
let label = displayLabel(labelY: labelY, slider: slider)
view.addSubview(slider)
let horizonalContraints =
NSLayoutConstraint(item: slider, attribute: .leadingMargin,
relatedBy: .equal, toItem: view,
attribute: .leadingMargin, multiplier: 1.0,
constant: 20)
let horizonal2Contraints =
NSLayoutConstraint(item: slider, attribute: .trailingMargin,
relatedBy: .equal, toItem: view,
attribute: .trailingMargin, multiplier: 1.0,
constant: -20)
let pinTop =
NSLayoutConstraint(item: slider, attribute: .top,
relatedBy: .equal, toItem: view,
attribute: .top, multiplier: 1.0,
constant: CGFloat(sliderY))
slider.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([horizonalContraints, horizonal2Contraints, pinTop])
slider.addTarget(self, action: #selector(WidgetsVC.sliderValueDidChange(_:)), for: .valueChanged)
}
private func displayLabel(labelY: Int, slider: UISlider) -> UILabel {
let label = UILabel()
label.text = "0"
labels = label
view.addSubview(label)
let temp = SliderWidget()
temp.setLable(label: label)
temp.setSlider(slider: slider)
sliderArray.append(temp)
let horizonalContraints =
NSLayoutConstraint(item: label, attribute: .leadingMargin,
relatedBy: .equal, toItem: view,
attribute: .leadingMargin, multiplier: 1.0,
constant: 20)
let pinTop =
NSLayoutConstraint(item: label, attribute: .top,
relatedBy: .equal, toItem: view,
attribute: .top, multiplier: 1.0,
constant: CGFloat(labelY))
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([horizonalContraints, pinTop])
return label
}
}
I am not sure what do you want for exactly but you could do it in this way
#objc func sliderValueDidChange(slider: UISlider) {
print(slider.tag)
let changedSliderWidget = sliderArray.filter { (sliderWidget) -> Bool in
return sliderWidget.getSlider() == slider
}.last
}
changedSliderWidget will contain the widget that you need
The easy way to identify each one is to set tag values:
displaySlider(sliderY: 100,labelY: 70, tag: 1)
displaySlider(sliderY: 200, labelY: 170, tag: 2)
And
private func displayLabel(labelY:Int,slider:UISlider, tag: Int)->UILabel{
let label=UILabel()
label.tag = tag
label.text="0"
....
So you can identify the sliders with their .tag value.

Adding UITableView programmatically

I think I have looked through a lot of questions like mine but nothing seems to work.
I am sending request to the server and in the moment I get all the data, I am starting to fill my ViewController programmatically. And all this happening in main_queue
This is the code of adding table:
if self.attachments.count > 0 {
docTableView = UITableView(frame: CGRect(x: 0.0, y: 0.0, width: self.myView.frame.width, height: 500.0), style: UITableViewStyle.Plain)
docTableView!.translatesAutoresizingMaskIntoConstraints = false
docTableView!.registerNib(UINib(nibName: "MenuCell", bundle: nil), forCellReuseIdentifier: "MenuCell")
self.myView.addSubview(docTableView!)
docTableView!.dataSource = self
docTableView!.delegate = self
self.myView.addConstraint(NSLayoutConstraint(item: docTableView!, attribute: .Top, relatedBy: .Equal, toItem: subviews!.last, attribute: .Bottom, multiplier: 1.0, constant: 5.0))
self.myView.addConstraint(NSLayoutConstraint(item: docTableView!, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
self.docTableView?.reloadData()
subviews?.append(docTableView!)
}
Then, I realized that two methods have been called : numberOfRowsInSection, heightForRowAtIndexPath and even the count of elements is greater than 0.
But cellForRowAtIndexPath is not being called and I guess that the reason that I do not see the tableView at all.
So how can I get to it?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.docTableView {
return attachments.count
}
else {
return self.notificationViewModel!.comments.count
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0 // I add this to show that its not zero
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == self.docTableView {
let object = attachments[indexPath.row]
UIApplication.sharedApplication().openURL(NSURL(string: object.Url!)!)
}
}
So commTableView is the same as docTableView. I need both of them and they have the same problem.
EDIT: I have this hierarchy: View->ScrollView->myView
EDIT2: My ViewController code. I have different types of data to add but all of it needs tables of attachments and comments
class NotificationViewController: UIViewController, MarkChosenDelegate, UITableViewDataSource, UITableViewDelegate {
//IBOutlets FROM STORYBOARD
#IBOutlet weak var myView: UIView!
#IBOutlet weak var headerStackView: UIStackView!
#IBOutlet weak var setMarkButton: UIButton!
#IBOutlet weak var placingWayCodeLabel: UILabel!
#IBOutlet weak var leftDaysLabel: UILabel!
#IBOutlet weak var typeLabel: UILabel!
#IBOutlet weak var regionLabel: UILabel!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var notificationNameLabel: UILabel!
#IBOutlet weak var markColorButton: UIButton!
var docTableView:UITableView?
var commTableView:UITableView?
var delegate:NewMarkSetProtocol?
var notificationViewModel: NotificationViewModel?
var attachments:[Attachment] = []
//FIELDS FOR SEGUE TO THE CUSTOMER
var customerGuid:String?
var customerName:String?
var inn:String?
var kpp:String?
let marks = DataClass.sharedInstance.marks
var viewUtils:ViewUtils?
var notificationItem: NotificationT? {
didSet {
self.setUpTheHeaderInformation()
}
}
//VIEW CONTROLLER LIFE CYCLES METHODS
override func viewDidLoad() {
super.viewDidLoad()
self.setUpTheHeaderInformation()
viewUtils = ViewUtils()
viewUtils?.showActivityIndicator(self.view)
notificationViewModel = NotificationViewModel()
notificationViewModel?.delegateComments = self
notificationViewModel?.delegateInformation = self
if (notificationItem != nil) {
if UsefulClass.isConnectedToNetwork() == true {
notificationViewModel!.getNotification(notificationItem!)
notificationViewModel!.getComments((notificationItem?.NotificationGuid)!)
} else {
notificationViewModel!.getCoreNotification(notificationItem!)
}
}
print(setMarkButton)
}
func setUpTheHeaderInformation() {
if let notificationT = self.notificationItem {
self.navigationItem.title = notificationT.OrderName
self.notificationItem?.IsRead = true
if let label = self.notificationNameLabel {
label.text = notificationT.OrderName
self.placingWayCodeLabel.text = notificationT.getPlacingWayId()
self.leftDaysLabel.text = notificationT.getLeft()
self.typeLabel.text = notificationT.getType()
if (marks.count != 0) {
var mark:MarkClass?
for i in 0..<marks.count {
if (marks[i].Id == notificationT.MarkId) {
mark = marks[i]
}
}
if let _mark = mark {
self.setMarkButton.setTitle(String(_mark.Name!), forState: .Normal)
self.markColorButton.hidden = false
self.markColorButton.backgroundColor = UsefulClass.colorWithHexString(_mark.Color!)
} else {
self.markColorButton.hidden = true
}
}
if let code = notificationT.RegionCode {
self.regionLabel.text = UsefulClass.regionByRegionCode(code)
}
}
}
}
//TABLE VIEW
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let count:Int = 2
if tableView == self.docTableView {
print(attachments.count)
return attachments.count
}
if tableView == self.commTableView {
return self.notificationViewModel!.comments.count
}
return count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == self.docTableView {
let object = attachments[indexPath.row]
UIApplication.sharedApplication().openURL(NSURL(string: object.Url!)!)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == self.docTableView {
let cell = tableView.dequeueReusableCellWithIdentifier("MenuCell", forIndexPath: indexPath) as! MenuCell
let object = attachments[indexPath.row]
let endIndex = object.FileName!.endIndex.advancedBy(-4)
let type:String = (object.FileName?.substringFromIndex(endIndex))!
cell.imageMark.image = notificationViewModel!.getImageForAttachment(type)
cell.name.text = object.FileName
cell.count.text = ""
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("CommentItemCell", forIndexPath: indexPath) as! CommentTableViewCell
let object = self.notificationViewModel!.comments[indexPath.row]
if let name = object.getCreatorName() {
cell.nameUser.text = name
}
cell.textComment.text = object.Text
//cell.imageUser.image =
cell.timeComment.text = object.getTime()
return cell
}
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
if tableView == self.commTableView {
return true
} else {
return false
}
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
Requests.deleteComment(notificationViewModel!.comments[indexPath.row].Id!, notificationGuid: (self.notificationItem?.NotificationGuid)!)
notificationViewModel?.comments.removeAtIndex(indexPath.row)
self.commTableView!.reloadData()
} else {
}
}
extension String {
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: CGFloat.max)
let boundingBox = self.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.height
}
}
extension NSAttributedString {
func heightWithConstrainedWidth(width: CGFloat) -> CGFloat {
let constraintRect = CGSize(width: width, height: CGFloat.max)
let boundingBox = self.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
return ceil(boundingBox.height)
}
func widthWithConstrainedHeight(height: CGFloat) -> CGFloat {
let constraintRect = CGSize(width: CGFloat.max, height: height)
let boundingBox = self.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
return ceil(boundingBox.width)
}
}
extension NotificationViewController:NotificationInformationUpdate {
func informationUpdate() {
var subviews:[UIView]? = [UIView]()
switch(notificationItem?.Type)! {
case 0:
let notification = notificationViewModel?.notification as! Notification_223
self.attachments = notification.attachments
if let name = notification.TenderPlanOrganisationName {
subviews = addTitleandValue("Заказчик", _value: name, _subviews: subviews!, numberOfLines: 0)
}
if let initialSum = notification.InitialSum {
subviews = addTitleandValue("Цена контракта", _value: initialSum, _subviews: subviews!, numberOfLines: 0)
} else if let maxPrice = notificationItem?.MaxPrice {
subviews = addTitleandValue("Цена контракта", _value: UsefulClass.getMaxPrice(maxPrice), _subviews: subviews!, numberOfLines: 0)
}
break
case 1:
let notification = notificationViewModel?.notification as! Notification_44
self.attachments = notification.attachments!
let customerNameTitle = UILabel()
customerNameTitle.text = "Заказчик:"
customerNameTitle.translatesAutoresizingMaskIntoConstraints = false
customerNameTitle.textColor = UIColor.grayColor()
setSimilarConstraintsToTitlesLabels(customerNameTitle, relatedView: self.headerStackView)
let customerName = UILabel()
customerName.text = notification.TenderPlanOrganisationName
customerName.textColor = UIColor.blueColor()
customerName.userInteractionEnabled = true
let tapGester = UITapGestureRecognizer(target: self, action: #selector(NotificationViewController.customerNameClick(_:)))
customerName.addGestureRecognizer(tapGester)
subviews = setSimilarConstraintsToValuesLabels(customerName, relatedView: customerNameTitle, _subViews: subviews!)
if let maxprice = notificationItem?.MaxPrice {
subviews = addTitleandValue("Цена контракта", _value: UsefulClass.getMaxPrice(maxprice), _subviews: subviews!, numberOfLines: 0)
}
break
case 2:
let notification = notificationViewModel?.notification as! B2BNotification
self.attachments = notification.attachments
subviews = addTitleandValue("Заказчик", _value: notification.TenderPlanOrganisationName!, _subviews: subviews!, numberOfLines: 0)
if let priceTotal = notification.PriceTotal {
var value = UsefulClass.getMaxPrice(priceTotal)
if let pricevat = notification.PriceVAT {
value.appendContentsOf(" (" + pricevat + ")")
}
subviews = addTitleandValue("Начальная цена всего лота", _value: value, _subviews: subviews!, numberOfLines: 0)
} else {
subviews = addTitleandValue("Начальная цена всего лота", _value: "Отсутствует поле", _subviews: subviews!, numberOfLines: 0)
}
if let priceone = notification.PriceOne {
subviews = addTitleandValue("Цена за единицу продукции", _value: UsefulClass.getMaxPrice(priceone), _subviews: subviews!, numberOfLines: 0)
}
break
case 7, 17:
let notification = notificationViewModel?.notification as! TakTorgNotification
self.attachments = notification.attachments
subviews = addTitleandValue("Наименование заказа", _value: notification.Subject!, _subviews: subviews!, numberOfLines: 0)
if let procNumber = notification.ProcedureProcedureNumber {
subviews = addTitleandValue("Номер закупки", _value: procNumber, _subviews: subviews!, numberOfLines: 0)
} else if let procNumber2 = notification.ProcedureProcedureNumber2 {
subviews = addTitleandValue("Номер закупки", _value: procNumber2, _subviews: subviews!, numberOfLines: 0)
}
if let startPrice = notification.StartPrice {
subviews = addTitleandValue("Начальная цена", _value: UsefulClass.getMaxPrice(startPrice), _subviews: subviews!, numberOfLines: 0)
}
if let peretorgPossible = notification.ProcedurePeretorgPossible {
subviews = addTitleandValue("Возможность проведения процедуры переторжки", _value: peretorgPossible, _subviews: subviews!, numberOfLines: 0)
}
if let negotiationPossible = notification.ProcedureNegotiationPossible {
subviews = addTitleandValue("Возможность проведения переговоров", _value: negotiationPossible, _subviews: subviews!, numberOfLines: 0)
}
//….
break
default:
break
}
let documentsTitle = UILabel()
documentsTitle.text = "Документы закупки"
documentsTitle.textColor = UIColor.blackColor()
documentsTitle.translatesAutoresizingMaskIntoConstraints = false
documentsTitle.font = documentsTitle.font.fontWithSize(18)
self.myView.addSubview(documentsTitle)
self.myView.addConstraint(NSLayoutConstraint(item: documentsTitle, attribute: .Top, relatedBy: .Equal, toItem: subviews!.last, attribute: .Bottom, multiplier: 1.0, constant: 12.0))
self.myView.addConstraint(NSLayoutConstraint(item: documentsTitle, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
subviews?.append(documentsTitle)
if self.attachments.count > 0 {
docTableView = UITableView()
docTableView!.translatesAutoresizingMaskIntoConstraints = false
docTableView!.registerNib(UINib(nibName: "MenuCell", bundle: nil), forCellReuseIdentifier: "MenuCell")
self.myView.addSubview(docTableView!)
docTableView!.dataSource = self
docTableView!.delegate = self
self.myView.addConstraint(NSLayoutConstraint(item: docTableView!, attribute: .Top, relatedBy: .Equal, toItem: subviews!.last, attribute: .Bottom, multiplier: 1.0, constant: 5.0))
self.myView.addConstraint(NSLayoutConstraint(item: docTableView!, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
self.myView.addConstraint(NSLayoutConstraint(item: docTableView!, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 300.0))
self.docTableView?.reloadData()
subviews?.append(docTableView!)
}
if notificationViewModel?.comments.count > 0 {
commTableView = UITableView()
commTableView?.translatesAutoresizingMaskIntoConstraints = false
commTableView!.registerNib(UINib(nibName: "CommentCell", bundle: nil), forCellReuseIdentifier: "CommentItemCell")
self.myView.addSubview(commTableView!)
self.myView.addConstraint(NSLayoutConstraint(item: commTableView!, attribute: .Top, relatedBy: .Equal, toItem: subviews!.last, attribute: .Bottom, multiplier: 1.0, constant: 5.0))
self.myView.addConstraint(NSLayoutConstraint(item: commTableView!, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
subviews?.append(commTableView!)
commTableView?.dataSource = self
commTableView?.delegate = self
}
//TITLE
let addCommentLabel = UILabel()
addCommentLabel.text = "Добавьте свой комментарий"
addCommentLabel.translatesAutoresizingMaskIntoConstraints = false
addCommentLabel.textColor = UIColor.lightGrayColor()
self.myView.addSubview(addCommentLabel)
self.myView.addConstraint(NSLayoutConstraint(item: addCommentLabel, attribute: .Top, relatedBy: .Equal, toItem: subviews!.last, attribute: .Bottom, multiplier: 1.0, constant: 10.0))
self.myView.addConstraint(NSLayoutConstraint(item: addCommentLabel, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
subviews?.append(addCommentLabel)
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.borderStyle = .RoundedRect
self.myView.addSubview(textField)
self.myView.addConstraint(NSLayoutConstraint(item: textField, attribute: .Top, relatedBy: .Equal, toItem: subviews!.last, attribute: .Bottom, multiplier: 1.0, constant: 5.0))
self.myView.addConstraint(NSLayoutConstraint(item: textField, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
self.myView.addConstraint(NSLayoutConstraint(item: textField, attribute: .Width, relatedBy: .Equal, toItem: self.myView, attribute: .Width, multiplier: 1.0, constant: -15))
subviews?.append(textField)
let sendButton = UIButton()
sendButton.setTitle("Отправить", forState: .Normal)
sendButton.translatesAutoresizingMaskIntoConstraints = false
sendButton.backgroundColor = UIColor.blueColor()
sendButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.myView.addSubview(sendButton)
self.myView.addConstraint(NSLayoutConstraint(item: sendButton, attribute: .Top, relatedBy: .Equal, toItem: subviews!.last, attribute: .Bottom, multiplier: 1.0, constant: 5.0))
self.myView.addConstraint(NSLayoutConstraint(item: textField, attribute: .Trailing, relatedBy: .Equal, toItem: textField, attribute: .Trailing, multiplier: 1.0, constant: 0))
subviews?.append(sendButton)
var height:CGFloat = 0.0
for i in 0..<self.myView.subviews.count {
height = height + myView.subviews[i].bounds.height
}
self.myView.frame = CGRect(x: 0, y: 0, width: self.myView.frame.width, height: self.myView.frame.height + height)
self.view.frame = CGRect(x: 0, y: 0, width: self.myView.frame.width, height: self.view.frame.height + height)
self.scrollView.frame = CGRect(x: 0, y: 0, width: self.myView.frame.width, height: self.scrollView.frame.height + height)
self.scrollView.contentSize = CGSize(width: self.myView.frame.width, height: self.scrollView.frame.height + height)
subviews = nil
self.viewUtils?.hideActivityIndicator(self.view)
}
func addPubDate(_subviews:[UIView], date:String, number:String) -> [UIView] {
var subviews = _subviews
let pubDateLabel = UILabel()
pubDateLabel.text = "Дата публикации: " + UsefulClass.covertDataWithZ(date, withTime: false)
pubDateLabel.translatesAutoresizingMaskIntoConstraints = false
pubDateLabel.textColor = UIColor.blackColor()
pubDateLabel.font = pubDateLabel.font.fontWithSize(11)
self.myView.addSubview(pubDateLabel)
self.myView.addConstraint(NSLayoutConstraint(item: pubDateLabel, attribute: .Top, relatedBy: .Equal, toItem: subviews.last, attribute: .Bottom, multiplier: 1.0, constant: 8.0))
self.myView.addConstraint(NSLayoutConstraint(item: pubDateLabel, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
subviews.append(pubDateLabel)
notificationUrlNumber(subviews, relatedView: pubDateLabel, number: number)
return subviews
}
func notificationUrlNumber(subviews:[UIView], relatedView:UILabel, number:String) {
let label = UILabel()
label.text = "Извещение №: " + number
label.textColor = UIColor.blueColor()
setSimilarConstraintsToValuesLabels(label, relatedView: relatedView, _subViews: subviews)
}
//левый заголовок для поля
func addtitle(_title:String, _subviews:[UIView]) -> [UIView] {
var subviews = _subviews
let title = UILabel()
title.text = _title
title.numberOfLines = 0
title.textColor = UIColor.blackColor()
title.translatesAutoresizingMaskIntoConstraints = false
title.font = title.font.fontWithSize(18)
self.myView.addSubview(title)
self.myView.addConstraint(NSLayoutConstraint(item: title, attribute: .Top, relatedBy: .Equal, toItem: subviews.last, attribute: .Bottom, multiplier: 1.0, constant: 12.0))
self.myView.addConstraint(NSLayoutConstraint(item: title, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
subviews.append(title)
return subviews
}
//правое значение для информационого поля
func addTitleandValue(_title:String, _value:String, _subviews:[UIView], numberOfLines:Int) -> [UIView] {
var subviews = _subviews
let title = UILabel()
title.text = _title
title.font = title.font.fontWithSize(12)
if subviews.count > 0 {
setSimilarConstraintsToTitlesLabels(title, relatedView: (subviews.last!))
} else {
setSimilarConstraintsToTitlesLabels(title, relatedView: self.headerStackView)
}
let value = UILabel()
value.text = _value
value.numberOfLines = numberOfLines
value.font = value.font.fontWithSize(12)
subviews = setSimilarConstraintsToValuesLabels(value, relatedView: title, _subViews: subviews)
return subviews
}
func addBoolEptrfValues(_title:String, _subviews:[UIView])->[UIView] {
var subviews = _subviews
let title = UILabel()
title.text = _title
title.numberOfLines = 0
setSimilarConstraintsToTitlesLabels(title, relatedView: (subviews.last!))
let value = UILabel()
value.text = notificationViewModel?.convertBoolToString(true)
value.textColor = UIColor.blackColor()
subviews = setSimilarConstraintsToValuesLabels(value, relatedView: title, _subViews: subviews)
return subviews
}
func addDeleteButton(subviews:[UIView], isDeleted:Bool) -> UILabel {
let deleteLabel = UILabel()
if isDeleted == false {
deleteLabel.text = "Удалить"
} else {
deleteLabel.text = "Восстановить"
}
deleteLabel.translatesAutoresizingMaskIntoConstraints = false
deleteLabel.textColor = UIColor.blueColor()
deleteLabel.font = deleteLabel.font.fontWithSize(11)
self.myView.addSubview(deleteLabel)
self.myView.addConstraint(NSLayoutConstraint(item: deleteLabel, attribute: .Top, relatedBy: .Equal, toItem: subviews.last, attribute: .Bottom, multiplier: 1.0, constant: 8.0))
self.myView.addConstraint(NSLayoutConstraint(item: deleteLabel, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
return deleteLabel
}
//right values next to the title
func setSimilarConstraintsToValuesLabels(subView:UILabel, relatedView:UILabel, _subViews:[UIView]) -> [UIView] {
var subViews = _subViews
subView.translatesAutoresizingMaskIntoConstraints = false
self.myView.addSubview(subView)
if (relatedView.text?.characters.count < 35 && subView.text?.characters.count < 30) {
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .Right, relatedBy: .Equal, toItem: self.myView, attribute: .Right, multiplier: 1.0, constant: -15))
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .Left, relatedBy: .Equal, toItem: relatedView, attribute: .Right, multiplier: 1.0, constant: 5))
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .FirstBaseline, relatedBy: .Equal, toItem: relatedView, attribute: .LastBaseline, multiplier: 1.0, constant: 0))
} else {
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .Top, relatedBy: .Equal, toItem: relatedView, attribute: .Bottom, multiplier: 1.0, constant: 8.0))
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 8.0))
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .Width, relatedBy: .Equal, toItem: self.myView, attribute: .Width, multiplier: 1.0, constant: -15))
self.myView.addConstraint(NSLayoutConstraint(item: relatedView, attribute: .Width, relatedBy: .Equal, toItem: self.myView, attribute: .Width, multiplier: 1.0, constant: -15))
subViews.append(relatedView)
}
subViews.append(subView)
return subViews
}
func setSimilarConstraintsToTitlesLabels(subView:UILabel, relatedView:UIView) {
subView.translatesAutoresizingMaskIntoConstraints = false
subView.textColor = UIColor.grayColor()
subView.numberOfLines = 0
self.myView.addSubview(subView)
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .Top, relatedBy: .Equal, toItem: relatedView, attribute: .Bottom, multiplier: 1.0, constant: 8.0))
self.myView.addConstraint(NSLayoutConstraint(item: subView, attribute: .Leading, relatedBy: .Equal, toItem: self.headerStackView, attribute: .Leading, multiplier: 1.0, constant: 0))
}
}
The method "informationUpdate()" is called when information for showing is parsed.
Your tableView has no height Constraint, so the height is set to 0.
Then no cell are visible => cellForRow never called :)
I think you should try to create more clear code. Do not use "blind" array of subviews; do not append views directly to subviews property, you already added it to view hierarchy; if you use autolayout (translatesAutoresizingMaskIntoConstraints = false) - don't try mix it with frames in single view, go and set up all constraints; use different background colors for different views to debug view hierarchy at runtime; go to Xcode Debug\View Debugging\Capture View Hierarchy for deep explore view hierarchy at runtime
Also, if numberOfRowsInSection did called by UITableView and return value > 0 (check it!), then cellForRowAtIndexPath must be called immediately by this UITableView since dataSource property is set. If you have more than one UITableView objects - check if there is no unexpected replaces.
P.S. if you from Russia I can help on Russian lang

Resources