Disclosure indicator in tableView cell isn't showing - swift

I have added cell.accessoryType = .disclosureIndicator in my cellForRowAt delegate, and I have also ticked it in the viewController UI options under "accessory". I also added margins to the tableView so that it's 0 in relation to the parent view (that is, the device).
I even set cell.accessoryView?.tintColor = UIColor.black in case something was wrong with the color. But, still, I can't see the little arrow on the right side of the cell. I'm using a default cell. And in other viewControllers that I have it works, but in this one it doesn't.
I checked the width for both the cell and the tableview and they are both 375.
It does work if I add a custom image instead, like this:
let img = UIImage.init(named: "arrow")
cell.accessoryView = UIImageView.init(image: img)
But, I want the standard arrow.
Any more tips on what could be the issue here?

If you're targeting iOS 13 this won't work. You can verify on iOS12 but I've faced this very problem earlier this year and the solution was to add a UIButton to the cell constraining it to the trailing side of the screen.
let disclosureButton = UIButton()
disclosureButton.setImage(UIImage(named: "arrow"), for: .normal)
cell.addSubview(disclosureButton)
disclosureButton.translatesAutoresizingMaskIntoConstraints = false
disclosureButton.trailingAnchor.constraint(equalTo: cell.trailingAnchor, constant: -10).isActive = true
disclosureButton.centerYAnchor.constraint(equalTo: cell.centerYAnchor, constant: 0).isActive = true
disclosureButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
disclosureButton.heightAnchor.constraint(equalToConstant: 25).isActive = true

Related

How can I add two images (one at the left and one on the right side) in a button in Xcode programaticly? I need also a title of a button

func setupButtonUI() {
if let detailsImage = UIImage(named: "detalji_icon") {
setImage(detailsImage, for: .normal)
contentHorizontalAlignment = .left
contentVerticalAlignment = .center
}
if let arrowImage = UIImage(named: "posalji_zahtev_black_icon") {
setImage(arrowImage, for: .normal)
contentHorizontalAlignment = .right
contentVerticalAlignment = .center
}
}/this is one of the things I tried, but whit this I get only the second image and its set in the center not right and the left image is hot even there/
UIButton has only one imageView, you can create custom button like in this answer
https://stackoverflow.com/a/43112735/13628690
Found the next solution: Made a custom UIView and added two images and label as a subview of customView. Did the constraints programaticly. Just added UITapGestureRecognizer (addGestureRecognizer) at the entire view and its all elements are clickable. The design and functionality are satisfied.

Swift tvOS - TabBar Trailing Accessory View Focus

The tabbar has a trailingAccessoryView that the documentation indicates can be used for some user actions. However, while I can put a view inside this accessory view, it does not get the focus at any point. Is there a means to allow this view to get the focus when swiping right from the tabbar?
let symbolConfig = UIImage.SymbolConfiguration(pointSize: 30, weight: .bold, scale: .large)
let guideButton = UIButton()
guideButton.setImage(UIImage(systemName: "list.bullet", withConfiguration: symbolConfig), for: .normal)
guideButton.addTarget(self, action: #selector(test), for: .primaryActionTriggered)
guideButton.translatesAutoresizingMaskIntoConstraints = false
tabBar.trailingAccessoryView.addSubview(guideButton)
guideButton.topAnchor.constraint(equalTo: tabBar.trailingAccessoryView.topAnchor).isActive = true
guideButton.trailingAnchor.constraint(equalTo: tabBar.trailingAccessoryView.trailingAnchor, constant: 0).isActive = true
As a note, I have tried setting: tabBar.trailingAccessoryView.isUserInteractionEnabled = true to no effect.
While not an exact answer, I wound up getting around this issue by adding the button to the tabbar's view rather than to the trailingAccessoryView. Not clear why the accessory view wouldn't work, but this does turn out to be a workable answer that does the job just as well.

How to display a UITextView programmatically?

I am trying to display a basic UITextView programmatically. The problem is, if I want to display a UILabel with the same constraints and settings, it's all fine, I can have it displayed. However, if I change it to a UITextView, it just disappears. I created an empty project, just to check it, but still, no avail, just does not display.
Here's my code that works fine for a UILabel:
view = UIView()
view.backgroundColor = .systemBackground
explanationView = UILabel()
explanationView.layer.zPosition = 1
explanationView.translatesAutoresizingMaskIntoConstraints = false
explanationView.numberOfLines = 10
view.addSubview(explanationView)
NSLayoutConstraint.activate([
explanationView.centerXAnchor.constraint(equalTo: view.layoutMarginsGuide.centerXAnchor),
explanationView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
explanationView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
explanationView.bottomAnchor.constraint(greaterThanOrEqualTo: view.layoutMarginsGuide.bottomAnchor, constant: -20)
])
I am using layer.zPosition to display it over a UIImageView. I also add .isScrollEnabled for the text view. I also tried creating the UITextView via a computed property, but did not have any luck either.
Any help is appreciated.
Edit: Below is the code that I use for the text view.
explanationView = UITextView()
explanationView.layer.zPosition = 1
explanationView.translatesAutoresizingMaskIntoConstraints = false
explanationView.isScrollEnabled = true
view.addSubview(explanationView)

Displaying buttons with programmatic constraints

Despite adding constraints on leading and top anchors for my buttons (I am trying to display them in a list), the view displays all of them in the top left corner of the view. I am calling many methods to try to apply the constraints, but none of them seem to be working. Help!
func displayButtonArray(buttons: [UIButton]) {
var previousButton: UIButton = buttons[0]
for (i, button) in buttons.enumerated() {
button.setTitle("SampleButton", for: .normal)
button.setTitleColor(.black, for: .normal)
button.sizeToFit()
let margins = self.view.layoutMarginsGuide
button.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
if i == 1 {
button.topAnchor.constraint(equalTo: margins.topAnchor, constant: CGFloat(5.0))
} else {
button.topAnchor.constraint(equalTo: previousButton.bottomAnchor, constant: CGFloat(10.0))
}
previousButton = button
button.translatesAutoresizingMaskIntoConstraints = false
button.setNeedsUpdateConstraints()
button.setNeedsLayout()
self.view.addSubview(button)
}
self.view.setNeedsUpdateConstraints()
self.view.setNeedsLayout()
self.view.layoutSubviews()
}
As you pointed out as well, constraints are defined but not activated.
One thing I can add is that as it's mentioned by Apple, AutoLayout constraints should be activated all together.
You may activate them one by one but it's more efficient to activate them all together; leading to better performance.

Setting constraints on UITableView inside UIView

I have a UIViewController that has some base controls and in the center has a UIView (as container of swappable ui controls). I swap out the UI controls in this center UIView (container) depending on what the user is trying to do. All of the UI controls that go in to the UIView container are defined programmatically and I use programatic constraints to place them inside the UIView container.
This works fine for all of the sets of UI controls I have done so far. Now I am adding a set of controls to the UIView container that includes a UITableView
I cant figure out how to get the TableView to show up inside the UIView programatically. I can define say a button and label and run the app and see the container with the button and the label. If I add the UITableView as below then the container just does not show up at all.
// tableView
tableView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: inputsContainerView.bottomAnchor).isActive = true
tableView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
tableView.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/2).isActive = true
prior to the above code I have already added all of the needed controls to the container subview ...
// Add controls to the view
inputsContainerView.addSubview(listTextView)
inputsContainerView.addSubview(listImageButton)
inputsContainerView.addSubview(listImageClear)
inputsContainerView.addSubview(tableView)
If I leave off the tableview then the container shows up with the other three fields. If I add the tableview then the container and all the other three controls are gone.
How do I add the tableView to the UIView and have it show up?
Here is how I defined the UITable view
let tableView: UITableView = {
let tv = UITableView()
return tv
}()
as a compare, when I define others controls like this they show up fine after adding to the subview and setting the constraints programatically
e.g.
// DEFINE
let listTextView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.text = ""
textView.textColor = defaultTextColor
textView.font = subtitleFont
textView.layer.borderColor = UIColor.black.cgColor
textView.layer.borderWidth = 1
textView.textAlignment = NSTextAlignment.left
return textView
}()
then later
// Place - with constraints
// listTextView
listTextView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: padFromLeft).isActive = true
listTextView.topAnchor.constraint(equalTo: inputsContainerView.topAnchor, constant: 10).isActive = true
listTextView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, multiplier: 9/16).isActive = true
listTextView.heightAnchor.constraint(equalTo: inputsContainerView.widthAnchor, multiplier: 5/16).isActive = true
Just added my comment as detailed answer, so others can see the solution faster and benefit from it.
So taken from the apple documentation, to set your own constraints programmatically, you need to set translatesAutoresizingMaskIntoConstraints to false:
Note that the autoresizing mask constraints fully specify the view’s size and position; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts. If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to false, and then provide a non ambiguous, nonconflicting set of constraints for the view.
So in your case you miss to set it for your table view, when you define it. Just add this line to it:
tv.translatesAutoresizingMaskIntoConstraints = false