UITableViewCell breaks layout when updating programmatic constraints - swift

I know that Stackoverflow is not a help desk, but I just can't figure out what's wrong here:
I am building a Twitter clone. I am using a class with programmatic constraints as a tableview cell. Since the tweets sometimes contain images, sometimes rich links and sometime just text, I want the cell to dynamically change. Here is the problem: if the cell contains an image, the layout randomly breaks, like so:
The debug navigator shows that there seems to be something wrong with the constraints of the image:
Height and position of the top row including the name and date also seem to be ambiguous. If I scroll, tweets containing images look like this:
In the cell init, I add all the subviews and call the setConstraints function:
func setConstraints() {
// User Image
userImageView.translatesAutoresizingMaskIntoConstraints = false
userImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: twentyOne).isActive = true
userImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: distanceToContentViewLeading).isActive = true
userImageView.heightAnchor.constraint(equalToConstant: relativeUserImageHeightAndWidth).isActive = true
userImageView.widthAnchor.constraint(equalToConstant: relativeUserImageHeightAndWidth).isActive = true
// User Name
userNameLabel.translatesAutoresizingMaskIntoConstraints = false
userNameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: twentyOne).isActive = true
userNameLabel.leadingAnchor.constraint(equalTo: userImageView.trailingAnchor, constant: relativeUserImageNameDistance).isActive = true
// Confirmed User
confirmedUserIcon.translatesAutoresizingMaskIntoConstraints = false
confirmedUserIcon.topAnchor.constraint(equalTo: contentView.topAnchor, constant: twentyOne).isActive = true
confirmedUserIcon.leadingAnchor.constraint(equalTo: userNameLabel.trailingAnchor, constant: 3).isActive = true
// Account Name
twitterAccountNameLabel.translatesAutoresizingMaskIntoConstraints = false
twitterAccountNameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: twentyOne).isActive = true
twitterAccountNameLabel.leadingAnchor.constraint(equalTo: confirmedUserIcon.trailingAnchor, constant: eleven).isActive = true
twitterAccountNameLabel.lineBreakMode = .byTruncatingTail
// Time Label
timeLabel.translatesAutoresizingMaskIntoConstraints = false
timeLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: twentyOne).isActive = true
timeLabel.leadingAnchor.constraint(equalTo: twitterAccountNameLabel.trailingAnchor, constant: eleven).isActive = true
timeLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: distanceToContentViewTrailing).isActive = true
timeLabel.sizeToFit()
// Bookmark Icon
bookmarkIcon.translatesAutoresizingMaskIntoConstraints = false
bookmarkIcon.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
bookmarkIcon.leadingAnchor.constraint(equalTo: likesCountLabel.trailingAnchor, constant: relativeDistanceBetweenCountAndNextIcon).isActive = true
bookmarkIcon.heightAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
bookmarkIcon.widthAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
// Tweet Text
tweetTextLabel.translatesAutoresizingMaskIntoConstraints = false
tweetTextLabel.topAnchor.constraint(equalTo: userNameLabel.bottomAnchor, constant: eleven).isActive = true
tweetTextLabel.leadingAnchor.constraint(equalTo: userNameLabel.leadingAnchor).isActive = true
tweetTextLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: distanceToContentViewTrailing).isActive = true
// Invisible Border
invisibleBorder.translatesAutoresizingMaskIntoConstraints = false
invisibleBorder.widthAnchor.constraint(equalToConstant: contentViewWidth).isActive = true
invisibleBorder.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
invisibleBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true
// Answer Icon
answerIcon.translatesAutoresizingMaskIntoConstraints = false
answerIcon.topAnchor.constraint(equalTo: invisibleBorder.bottomAnchor, constant: twentyOne).isActive = true
answerIcon.leadingAnchor.constraint(equalTo: userNameLabel.leadingAnchor).isActive = true
answerIcon.heightAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
answerIcon.widthAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
// Answer Count
answerCountLabel.translatesAutoresizingMaskIntoConstraints = false
answerCountLabel.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
answerCountLabel.leadingAnchor.constraint(equalTo: answerIcon.trailingAnchor, constant: eleven).isActive = true
// Retweet Icon
retweetIcon.translatesAutoresizingMaskIntoConstraints = false
retweetIcon.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
retweetIcon.leadingAnchor.constraint(equalTo: answerCountLabel.trailingAnchor, constant: relativeDistanceBetweenCountAndNextIcon).isActive = true
retweetIcon.heightAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
retweetIcon.widthAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
// Retweet Count
retweetCountLabel.translatesAutoresizingMaskIntoConstraints = false
retweetCountLabel.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
retweetCountLabel.leadingAnchor.constraint(equalTo: retweetIcon.trailingAnchor, constant: eleven).isActive = true
// Likes Icon
likesIcon.translatesAutoresizingMaskIntoConstraints = false
likesIcon.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
likesIcon.leadingAnchor.constraint(equalTo: retweetCountLabel.trailingAnchor, constant: relativeDistanceBetweenCountAndNextIcon).isActive = true
likesIcon.heightAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
likesIcon.widthAnchor.constraint(equalToConstant: iconSizeiPhone).isActive = true
// Likes Count
likesCountLabel.translatesAutoresizingMaskIntoConstraints = false
likesCountLabel.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
likesCountLabel.leadingAnchor.constraint(equalTo: likesIcon.trailingAnchor, constant: eleven).isActive = true
// Share Icon
shareIcon.translatesAutoresizingMaskIntoConstraints = false
shareIcon.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
shareIcon.trailingAnchor.constraint(equalTo: trashIcon.leadingAnchor, constant: -eleven).isActive = true
shareIcon.heightAnchor.constraint(equalToConstant: iconSizeiPhone - 3).isActive = true
shareIcon.widthAnchor.constraint(equalToConstant: iconSizeiPhone - 3).isActive = true
// Trash Icon
trashIcon.translatesAutoresizingMaskIntoConstraints = false
trashIcon.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
trashIcon.trailingAnchor.constraint(equalTo: ellipsisIcon.leadingAnchor, constant: -eleven).isActive = true
trashIcon.heightAnchor.constraint(equalToConstant: iconSizeiPhone - 3 ).isActive = true
trashIcon.widthAnchor.constraint(equalToConstant: iconSizeiPhone - 3).isActive = true
// Ellipsis Icon
ellipsisIcon.translatesAutoresizingMaskIntoConstraints = false
ellipsisIcon.centerYAnchor.constraint(equalTo: answerIcon.centerYAnchor).isActive = true
ellipsisIcon.trailingAnchor.constraint(equalTo: tweetTextLabel.trailingAnchor).isActive = true
ellipsisIcon.heightAnchor.constraint(equalToConstant: iconSizeiPhone - 3).isActive = true
ellipsisIcon.widthAnchor.constraint(equalToConstant: iconSizeiPhone - 3).isActive = true
// Bottom Border
bottomBorder.translatesAutoresizingMaskIntoConstraints = false
bottomBorder.topAnchor.constraint(equalTo: answerIcon.bottomAnchor, constant: twentyOne ).isActive = true
bottomBorder.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: distanceToContentViewTrailing).isActive = true
bottomBorder.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: distanceToContentViewLeading).isActive = true
bottomBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true
//bottomBorder.widthAnchor.constraint(equalToConstant: 75).isActive = true
bottomBorder.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -eleven).isActive = true
}
When dequeuing the reusable cell, I also call the setImageOrRichLinkConstraintsFunction to update the constraints:
func setImageOrRichLinkConstraints(postImage: UIImage?, link: String?, imageWidth: CGFloat, imageHeight: CGFloat) {
// Tweet Image
if postImage != nil {
contentView.addSubview(tweetImageView)
tweetImageView.translatesAutoresizingMaskIntoConstraints = false
tweetImageTopAnchor = tweetImageView.topAnchor.constraint(equalTo: tweetTextLabel.bottomAnchor, constant: twentyOne)
tweetImageTopAnchor.isActive = true
tweetImageTrailingAnchor = tweetImageView.trailingAnchor.constraint(equalTo: tweetTextLabel.trailingAnchor)
tweetImageTrailingAnchor.isActive = true
tweetImageLeadingAnchor = tweetImageView.leadingAnchor.constraint(equalTo: tweetTextLabel.leadingAnchor)
tweetImageLeadingAnchor.isActive = true
tweetImageHeightAchor = tweetImageView.heightAnchor.constraint(equalToConstant: (imageHeight))
tweetImageHeightAchor.isActive = true
tweetImageWidthAnchor = tweetImageView.widthAnchor.constraint(equalToConstant: (imageWidth))
tweetImageWidthAnchor.isActive = true
invisibleBoarderToImage = invisibleBorder.topAnchor.constraint(equalTo: tweetImageView.bottomAnchor)
invisibleBoarderToImage.isActive = true
}
else if link != nil{
contentView.addSubview(linkPreview)
linkPreview.translatesAutoresizingMaskIntoConstraints = false
linkPreviewTopConstraint = linkPreview.topAnchor.constraint(equalTo: tweetTextLabel.bottomAnchor, constant: twentyOne)
linkPreviewTopConstraint.isActive = true
linkPreviewTrainlingConstraint = linkPreview.trailingAnchor.constraint(equalTo: tweetTextLabel.trailingAnchor)
linkPreviewTrainlingConstraint.isActive = true
linkPreviewHeightConstraint = linkPreview.heightAnchor.constraint(equalToConstant: contentViewWidth - (distanceToContentViewLeading + relativeUserImageHeightAndWidth + relativeUserImageNameDistance + distanceToContentViewTrailing) * (16/9))
linkPreviewHeightConstraint.isActive = true
linkPreview.leadingAnchor.constraint(equalTo: userNameLabel.leadingAnchor).isActive = true
invisibleBorderToLinkPreview = invisibleBorder.topAnchor.constraint(equalTo: linkPreview.bottomAnchor)
invisibleBorderToLinkPreview.isActive = true
}
else {
invisibleBorderToTweetText = invisibleBorder.topAnchor.constraint(equalTo: tweetTextLabel.bottomAnchor)
invisibleBorderToTweetText.isActive = true
}
self.layoutIfNeeded()
}

Related

Scrollview not scrolling / Trying to add view to bottom of content

So I've added a scrollview to my view controller programmatically and added some views.
func setupScrollView() {
view.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
scrollView.contentSize = contentView.frame.size
}
func addUserAndFollowView(id: String) {
userAndFollow = UserPfAndFollow(id: id)
if let userAndFollow = userAndFollow {
userAndFollow.view.isUserInteractionEnabled = true
contentView.addSubview(userAndFollow.view)
self.contentView.bringSubviewToFront(userAndFollow.view)
userAndFollow.view.translatesAutoresizingMaskIntoConstraints = false
userAndFollow.view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
userAndFollow.view.widthAnchor.constraint(equalTo: contentView.widthAnchor).isActive = true
userAndFollow.view.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
}
func setImageViewConstraints() {
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 300).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 300).isActive = true
imageView.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor).isActive = true
if let userAndFollow = userAndFollow?.view {
imageView.topAnchor.constraint(equalTo: userAndFollow.bottomAnchor, constant: 5).isActive = true
}
}
func addLabelConstraints() {
self.albumTitle?.translatesAutoresizingMaskIntoConstraints = false
self.albumDescription?.translatesAutoresizingMaskIntoConstraints = false
albumTitle?.numberOfLines = 2
albumDescription?.numberOfLines = 3
albumDescription?.adjustsFontSizeToFitWidth = false
albumTitle?.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 14).isActive = true
albumTitle?.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10).isActive = true
albumTitle?.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
albumTitle?.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10).isActive = true
albumDescription?.lineBreakMode = .byTruncatingTail
albumDescription?.topAnchor.constraint(equalTo: albumTitle!.bottomAnchor, constant: 9).isActive = true
albumDescription?.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10).isActive = true
albumDescription?.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
}
All these views show up the way I want them to with the scrollview not scrolling but when I add this next view (which is a tableview in a view controller) at the bottom of the rest of my views it doesn't show up. Possibly why the scrollview isn't scrolling.
func addViewController() {
if let viewController = viewController {
contentView.addSubview(viewController.view)
setVCConstraints()
}
}
func setVCConstraints() {
viewController?.view.translatesAutoresizingMaskIntoConstraints = false
viewController?.view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
viewController?.view.topAnchor.constraint(equalTo: albumDescription!.bottomAnchor, constant: 7).isActive = true
viewController?.view.widthAnchor.constraint(equalTo: self.contentView.widthAnchor).isActive = true
}
What can I do to make this view appear and have my scrollview scroll all the way down this view controller and it's array content and no more or less?
Before you add the view, you have
contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
scrollView.contentSize = contentView.frame.size
So, of course, the contentView has a size of the scrollView and thus will not scroll. It is the same size.
After you add the view, you have
viewController?.view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
viewController?.view.topAnchor.constraint(equalTo: albumDescription!.bottomAnchor, constant: 7).isActive = true
viewController?.view.widthAnchor.constraint(equalTo: self.contentView.widthAnchor).isActive = true
Note, above, that you define a top bottom and width -- there is no LEFT-TO-RIGHT indication where the viewController.view should start on the horizontal path. This can result in some VERY weird behaviors.
Yet, you STILL don't modify the contentView.contentSize to be any bigger than the scrollView size.
To make something scroll, you need the contentView.contentSize to be bigger than the frame.size.

UIStackView with custom size elements and item in the center - Swift - Programmatically

I want to set a UIStackView with 3 views to the bottom of my so that the centre view is positioned in the centre and the other views are adjusted accordingly, I want also to be able to set the width and height anchors for all 3 views.
This is the desired outcome:
This is what I'm getting:
This is the code I'm using:
bottomStackView = UIStackView(arrangedSubviews: [pickerView, downloadContentButton, shareButton])
bottomStackView.alignment = .center
bottomStackView.distribution = .fill
bottomStackView.axis = .horizontal
bottomStackView.spacing = 5
bottomStackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bottomStackView)
bottomStackView.anchor(top: nil, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor)
bottomStackView.heightAnchor.constraint(equalToConstant: 150).isActive = true
bottomStackView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
pickerView.heightAnchor.constraint(equalToConstant: 120).isActive = true
shareButton.heightAnchor.constraint(equalToConstant: 150).isActive = true
shareButton.widthAnchor.constraint(equalTo: pickerView.widthAnchor).isActive = true
downloadContentButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
downloadContentButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
shareButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
shareButton.heightAnchor.constraint(equalToConstant: 80).isActive = true
I think you are after this kind of thing:
Or, if we get wider:
If that's the idea, then one key mistake is when you say:
bottomStackView.distribution = .fill
You actually want .equalDistribution. To demonstrate, I created the example entirely in code, so you can see all the settings:
let sv = UIStackView()
sv.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(sv)
sv.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20).isActive = true
sv.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20).isActive = true
sv.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20).isActive = true
sv.heightAnchor.constraint(equalToConstant: 160).isActive = true
sv.distribution = .equalSpacing
sv.alignment = .center
let v1 = UIView()
v1.translatesAutoresizingMaskIntoConstraints = false
v1.widthAnchor.constraint(equalToConstant: 120).isActive = true
v1.heightAnchor.constraint(equalToConstant: 150).isActive = true
v1.backgroundColor = .red
sv.addArrangedSubview(v1)
let v2 = UIView()
v2.translatesAutoresizingMaskIntoConstraints = false
v2.widthAnchor.constraint(equalToConstant: 80).isActive = true
v2.heightAnchor.constraint(equalToConstant: 80).isActive = true
v2.backgroundColor = .yellow
sv.addArrangedSubview(v2)
let v3 = UIView()
v3.translatesAutoresizingMaskIntoConstraints = false
v3.widthAnchor.constraint(equalToConstant: 120).isActive = true
v3.heightAnchor.constraint(equalToConstant: 150).isActive = true
v3.backgroundColor = .blue
sv.addArrangedSubview(v3)

Adding Image to view in swift

I am trying to add an image to a subview. but whenever I run a simulation I cant see the image while I have label that shows up on the same view
let View:UIView = UIView()
let CheckImageView:UIImageView = UIImageView(image: UIImage(named: "Checked 1"))
view.addSubview(View)
View.backgroundColor = .white
View.translatesAutoresizingMaskIntoConstraints = false
View.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor,constant: -20).isActive = true
View.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
View.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,constant: 0).isActive = true
View.heightAnchor.constraint(equalToConstant: 200).isActive = true
View.layer.cornerRadius = 25
View.layer.shadowOpacity = 0.15
View.layer.shadowRadius = 20
View.layer.shadowOffset = .zero
View.layer.shadowColor = UIColor.black.cgColor
View.addSubview(CheckImageView)
CheckImageView.translatesAutoresizingMaskIntoConstraints = false
CheckImageView.heightAnchor.constraint(equalTo: CheckImageView.widthAnchor).isActive = true
CheckImageView.topAnchor.constraint(equalTo: View.topAnchor, constant: 30).isActive = true
CheckImageView.centerXAnchor.constraint(equalTo: View.centerXAnchor).isActive = true
CheckImageView.widthAnchor.constraint(equalToConstant: 50).isActive = true
It seems like your image size is smaller than expected.
Try to change your image width Anchor
CheckImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true

Multiple buttons triggered with one function programmaticaly swift

Tried to summarize my code. Problem is i am getting "newCalculator[2861:53607] -[UIButton buttonClicked:]: unrecognized selector sent to instance 0x7fd912651e50" error and trying to understand what i need to do? I researched some solutions changing selector syntax however still this problem persists. What i want is; inside a stackview i add multiple buttons with an order and all buttons should call same function when they are clicked.
o//
// ViewController.swift
// newCalculator
//
// Created by taylank on 17.10.2019.
// Copyright © 2019 TKT. All rights reserved.
//
import UIKit
var stackViews = [UIStackView]()
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Layer 1
let containerStackView1 = UIStackView()
containerStackView1.translatesAutoresizingMaskIntoConstraints = false
containerStackView1.axis = .vertical
containerStackView1.distribution = .fillEqually
containerStackView1.spacing = 1
//Layer 2
let topHalfView2 = UIView()
topHalfView2.translatesAutoresizingMaskIntoConstraints = false
topHalfView2.backgroundColor = .white
let botHalfView2 = UIView()
botHalfView2.translatesAutoresizingMaskIntoConstraints = false
//Layer 3
let topLabel3 = UILabel()
topLabel3.translatesAutoresizingMaskIntoConstraints = false
topLabel3.text = "0"
topLabel3.font = UIFont.boldSystemFont(ofSize: 40)
let containerStackView3 = UIStackView()
containerStackView3.translatesAutoresizingMaskIntoConstraints = false
containerStackView3.axis = .vertical
containerStackView3.distribution = .fillEqually
containerStackView3.spacing = 1
//Layer 4
stackViews = createStackViews(from: 0, to: 4, align: "h")
let yamaView = UIView()
yamaView.translatesAutoresizingMaskIntoConstraints = false
yamaView.backgroundColor = .black
//ADDING VIEWS TO SCENE .VIEW->Contstckview1->Top-BotHalfView->contstckview2-ResultLabel->horzstackviews
containerStackView1.addArrangedSubview(topHalfView2)
containerStackView1.addArrangedSubview(botHalfView2)
view.addSubview(containerStackView1)
topHalfView2.addSubview(topLabel3)
botHalfView2.addSubview(containerStackView3)
containerStackView3.addArrangedSubview(stackViews[0])
view.addSubview(yamaView)
//stackViews[0].topAnchor.constraint(equalTo: containerStackView3.topAnchor, constant: 0).isActive = true
for a in 1...stackViews.count-1{
containerStackView3.addArrangedSubview(stackViews[a])
// stackViews[a].leadingAnchor.constraint(equalTo: stackViews[0].leadingAnchor, constant: 0).isActive = true
// stackViews[a].trailingAnchor.constraint(equalTo: stackViews[0].trailingAnchor, constant: 0).isActive = true
}
//stackViews[0].leadingAnchor.constraint(equalTo: containerStackView3.leadingAnchor, constant: 0).isActive = true
// stackViews[0].trailingAnchor.constraint(equalTo: containerStackView3.trailingAnchor, constant: 0).isActive = true
//Uiheight--v:?--uibottom--uifillequ
//ConstraintsStackView1
containerStackView1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
containerStackView1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
containerStackView1.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
containerStackView1.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
//TopHalfView2
topHalfView2.topAnchor.constraint(equalTo: containerStackView1.topAnchor, constant: 0).isActive = true
topHalfView2.leadingAnchor.constraint(equalTo: containerStackView1.leadingAnchor, constant: 0).isActive = true
topHalfView2.trailingAnchor.constraint(equalTo: containerStackView1.trailingAnchor, constant: 0).isActive = true
// topHalfView2.heightAnchor.constraint(equalToConstant: containerStackView1.frame.height/2).isActive = true
//creates problem conflicts with other constraint fill equally?
//BotHalfView2
botHalfView2.bottomAnchor.constraint(equalTo: containerStackView1.bottomAnchor, constant: 0).isActive = true
botHalfView2.leadingAnchor.constraint(equalTo: containerStackView1.leadingAnchor, constant: 0).isActive = true
botHalfView2.trailingAnchor.constraint(equalTo: containerStackView1.trailingAnchor, constant: 0).isActive = true
// botHalfView2.heightAnchor.constraint(equalToConstant: containerStackView1.frame.height/2).isActive = true
//topLabel3
topLabel3.bottomAnchor.constraint(equalTo: topHalfView2.bottomAnchor, constant: 0).isActive = true
topLabel3.trailingAnchor.constraint(equalTo: topHalfView2.trailingAnchor, constant: -5).isActive = true
//ConstraintsStackView3
containerStackView3.leadingAnchor.constraint(equalTo: botHalfView2.leadingAnchor, constant: 0).isActive = true
containerStackView3.trailingAnchor.constraint(equalTo: botHalfView2.trailingAnchor, constant: 0).isActive = true
containerStackView3.topAnchor.constraint(equalTo: botHalfView2.topAnchor, constant: 0).isActive = true
containerStackView3.bottomAnchor.constraint(equalTo: botHalfView2.bottomAnchor, constant: 0).isActive = true
//constraint button
stackViews[4].arrangedSubviews[2].widthAnchor.constraint(equalTo: stackViews[4].arrangedSubviews[1].widthAnchor,multiplier: 1).isActive = true //sonbutton bir yandakine
stackViews[4].arrangedSubviews[1].widthAnchor.constraint(equalTo: stackViews[4].arrangedSubviews[0].widthAnchor,multiplier: 0.5).isActive = true // ortadaki buton en soldakine
yamaView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
yamaView.topAnchor.constraint(equalTo: stackViews[4].bottomAnchor).isActive = true
yamaView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
yamaView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
}
func createButtons(_ named: String...)->[UIButton]{
var c = 0
return named.map { name in
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(name, for: .normal)
button.backgroundColor = UIColor.gray
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 1
button.addTarget(button, action: #selector(buttonClicked) ,for:.touchUpInside)
return button
}
}
#objc func buttonClicked(_ sender:UIButton){
print("clicked")
}
func createStackViews(from x: Int,to y:Int, align type: String)->[UIStackView]{
// use tag instead of name---
//use dictionary..keys name values
if (type == "v"){
for a in x...y{
let stackVertical = UIStackView()
stackVertical.translatesAutoresizingMaskIntoConstraints = false
stackVertical.axis = .vertical
stackVertical.spacing = 1
stackVertical.distribution = .fillEqually
stackVertical.tag = a
}
}
else if(type=="h"){
var counter = 0
for a in x...y{
counter += 1
switch counter{
case 1: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("Clear","+/-","%","÷"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 90+a
stackViews.append(stackHorizontal)
case 2: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("7","8","9","x"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 100 + a
stackViews.append(stackHorizontal)
case 3: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("4","5","6","-"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 110+a
stackViews.append(stackHorizontal)
case 4: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("1","2","3","+"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 120 + a
stackViews.append(stackHorizontal)
case 5: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("0",",","="))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillProportionally
stackHorizontal.tag = 130 + a
stackViews.append(stackHorizontal)
default:
break
}
}
return stackViews
}
return [UIStackView.init()]
}
------------SOLUTION----------
All of the functions are inside the viewcontroller class
class ViewController: UIViewController {
override func viewDidLoad() {
stackViews = createStackViews(from: 0, to: 4, align: "h")
}
func createButtons(_ named: String...)->[UIButton]{
//codes
button.addTarget(self, action: #selector(buttonClicked) ,for:.touchUpInside)
//codes
}
#objc func buttonClicked(_ sender:UIButton){
print("clicked")
}
func createStackViews(from x: Int,to y:Int, align type: String)->[UIStackView]{
//codes
UIStackView(arrangedSubviews:createButtons("Clear","+/-","%","÷"))
//codes
}
}
You have several problems:
Your selector needs to be selector(buttonClicked(:)), so
button.addTarget(self, action: #selector(buttonClicked(:)) ,for:.touchUpInside)
Your functions buttonClicked(_:) and createButtons() need to be inside your view controller class. As it is, buttonClicked(_:) is a global function, which means it can't be part of a target/action. (Action methods need to be inside a target object (usually the view controller that owns the button.)
Replace
button.addTarget(button, action: "buttonClicked:" ,for:.touchUpInside)
with
button.addTarget(self, action: #selector(buttonClicked) ,for:.touchUpInside)
#objc func buttonClicked(_ sender:UIButton){
import UIKit
var stackViews = [UIStackView]()
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Layer 1
let containerStackView1 = UIStackView()
containerStackView1.translatesAutoresizingMaskIntoConstraints = false
containerStackView1.axis = .vertical
containerStackView1.distribution = .fillEqually
containerStackView1.spacing = 1
//Layer 2
let topHalfView2 = UIView()
topHalfView2.translatesAutoresizingMaskIntoConstraints = false
topHalfView2.backgroundColor = .white
let botHalfView2 = UIView()
botHalfView2.translatesAutoresizingMaskIntoConstraints = false
//Layer 3
let topLabel3 = UILabel()
topLabel3.translatesAutoresizingMaskIntoConstraints = false
topLabel3.text = "0"
topLabel3.font = UIFont.boldSystemFont(ofSize: 40)
let containerStackView3 = UIStackView()
containerStackView3.translatesAutoresizingMaskIntoConstraints = false
containerStackView3.axis = .vertical
containerStackView3.distribution = .fillEqually
containerStackView3.spacing = 1
//Layer 4
stackViews = createStackViews(from: 0, to: 4, align: "h")
let yamaView = UIView()
yamaView.translatesAutoresizingMaskIntoConstraints = false
yamaView.backgroundColor = .black
//ADDING VIEWS TO SCENE .VIEW->Contstckview1->Top-BotHalfView->contstckview2-ResultLabel->horzstackviews
containerStackView1.addArrangedSubview(topHalfView2)
containerStackView1.addArrangedSubview(botHalfView2)
view.addSubview(containerStackView1)
topHalfView2.addSubview(topLabel3)
botHalfView2.addSubview(containerStackView3)
containerStackView3.addArrangedSubview(stackViews[0])
view.addSubview(yamaView)
//stackViews[0].topAnchor.constraint(equalTo: containerStackView3.topAnchor, constant: 0).isActive = true
for a in 1...stackViews.count-1{
containerStackView3.addArrangedSubview(stackViews[a])
// stackViews[a].leadingAnchor.constraint(equalTo: stackViews[0].leadingAnchor, constant: 0).isActive = true
// stackViews[a].trailingAnchor.constraint(equalTo: stackViews[0].trailingAnchor, constant: 0).isActive = true
}
//stackViews[0].leadingAnchor.constraint(equalTo: containerStackView3.leadingAnchor, constant: 0).isActive = true
// stackViews[0].trailingAnchor.constraint(equalTo: containerStackView3.trailingAnchor, constant: 0).isActive = true
//Uiheight--v:?--uibottom--uifillequ
//ConstraintsStackView1
containerStackView1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
containerStackView1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
containerStackView1.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
containerStackView1.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
//TopHalfView2
topHalfView2.topAnchor.constraint(equalTo: containerStackView1.topAnchor, constant: 0).isActive = true
topHalfView2.leadingAnchor.constraint(equalTo: containerStackView1.leadingAnchor, constant: 0).isActive = true
topHalfView2.trailingAnchor.constraint(equalTo: containerStackView1.trailingAnchor, constant: 0).isActive = true
// topHalfView2.heightAnchor.constraint(equalToConstant: containerStackView1.frame.height/2).isActive = true
//creates problem conflicts with other constraint fill equally?
//BotHalfView2
botHalfView2.bottomAnchor.constraint(equalTo: containerStackView1.bottomAnchor, constant: 0).isActive = true
botHalfView2.leadingAnchor.constraint(equalTo: containerStackView1.leadingAnchor, constant: 0).isActive = true
botHalfView2.trailingAnchor.constraint(equalTo: containerStackView1.trailingAnchor, constant: 0).isActive = true
// botHalfView2.heightAnchor.constraint(equalToConstant: containerStackView1.frame.height/2).isActive = true
//topLabel3
topLabel3.bottomAnchor.constraint(equalTo: topHalfView2.bottomAnchor, constant: 0).isActive = true
topLabel3.trailingAnchor.constraint(equalTo: topHalfView2.trailingAnchor, constant: -5).isActive = true
//ConstraintsStackView3
containerStackView3.leadingAnchor.constraint(equalTo: botHalfView2.leadingAnchor, constant: 0).isActive = true
containerStackView3.trailingAnchor.constraint(equalTo: botHalfView2.trailingAnchor, constant: 0).isActive = true
containerStackView3.topAnchor.constraint(equalTo: botHalfView2.topAnchor, constant: 0).isActive = true
containerStackView3.bottomAnchor.constraint(equalTo: botHalfView2.bottomAnchor, constant: 0).isActive = true
//constraint button
stackViews[4].arrangedSubviews[2].widthAnchor.constraint(equalTo: stackViews[4].arrangedSubviews[1].widthAnchor,multiplier: 1).isActive = true //sonbutton bir yandakine
stackViews[4].arrangedSubviews[1].widthAnchor.constraint(equalTo: stackViews[4].arrangedSubviews[0].widthAnchor,multiplier: 0.5).isActive = true // ortadaki buton en soldakine
yamaView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
yamaView.topAnchor.constraint(equalTo: stackViews[4].bottomAnchor).isActive = true
yamaView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
yamaView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
func createButtons(_ named: String...)->[UIButton]{
var c = 0
return named.map { name in
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(name, for: .normal)
button.backgroundColor = UIColor.gray
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 1
button.addTarget(self, action: #selector(buttonClicked) ,for:.touchUpInside)
return button
}
}
#objc func buttonClicked(_ sender:UIButton){
print("clicked")
}
func createStackViews(from x: Int,to y:Int, align type: String)->[UIStackView]{
// use tag instead of name---
//use dictionary..keys name values
if (type == "v"){
for a in x...y{
let stackVertical = UIStackView()
stackVertical.translatesAutoresizingMaskIntoConstraints = false
stackVertical.axis = .vertical
stackVertical.spacing = 1
stackVertical.distribution = .fillEqually
stackVertical.tag = a
}
}
else if(type=="h"){
var counter = 0
for a in x...y{
counter += 1
switch counter{
case 1: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("Clear","+/-","%","÷"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 90+a
stackViews.append(stackHorizontal)
case 2: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("7","8","9","x"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 100 + a
stackViews.append(stackHorizontal)
case 3: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("4","5","6","-"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 110+a
stackViews.append(stackHorizontal)
case 4: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("1","2","3","+"))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillEqually
stackHorizontal.tag = 120 + a
stackViews.append(stackHorizontal)
case 5: let stackHorizontal = UIStackView(arrangedSubviews:createButtons("0",",","="))
stackHorizontal.translatesAutoresizingMaskIntoConstraints = false
stackHorizontal.axis = .horizontal
stackHorizontal.spacing = 1
stackHorizontal.distribution = .fillProportionally
stackHorizontal.tag = 130 + a
stackViews.append(stackHorizontal)
default:
break
}
}
return stackViews
}
return [UIStackView.init()]
}
}

UITextField inside UIScrollView freeze the app when click on the textfield to edit

swifters!
I am trying to add a many textfields inside a scrollview , and its working fine and the scrollview is scrolling just fine, but when I click on the any textfield to type any text he app freezing.
and I got this message on debug area XPC connection interrupted
can anyone help me!
here is my code for creating the scrollview and for the dynamic textfields
func setupScrollView() {
scrollView.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.centerXAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: container.widthAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor , multiplier: 0.75).isActive = true
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
}
this is how I am adding the textfields :
let questionContainer = UIView()
questionContainer.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(questionContainer)
if latestView == nil {
questionContainer.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
}
else {
questionContainer.topAnchor.constraint(equalTo: latestView!.bottomAnchor, constant: 32).isActive = true
}
questionContainer.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
questionContainer.widthAnchor.constraint(equalTo: contentView.widthAnchor).isActive = true
questionContainer.heightAnchor.constraint(equalToConstant: 100).isActive = true
questionContainer.backgroundColor = .red
let question = UITextField()
question.translatesAutoresizingMaskIntoConstraints = false
questionContainer.addSubview(question)
question.text = "Test Test Test"
question.topAnchor.constraint(equalTo: questionContainer.topAnchor, constant: 8).isActive = true
question.trailingAnchor.constraint(equalTo: questionContainer.trailingAnchor, constant: -8).isActive = true
question.bottomAnchor.constraint(equalTo: questionContainer.bottomAnchor, constant: -8).isActive = true
question.leadingAnchor.constraint(equalTo: questionContainer.leadingAnchor, constant: 8).isActive = true