How add Size and Position to ImageView in UITableView Cell? - swift

I added a image to my UITableView Cell:
cell.imageView!.image = UIImage(named: "Default.png")
Everything works, the image is displayed. Now I want to give a custom size and position for the image, so I try it with this code:
cell.imageView!.frame = CGRect(x: 29, y: 17, width: 14, height: 13)
But for any reason it doesn't work. Any advices?

If your position will not change You can create frame in your TableViewCell
class TableViewCell: UITableViewCell {
#IBOutlet weak var imageView: UIImageView!
override func layoutSubviews() {
super.layoutSubviews()
self.imageView?.frame = CGRect(x: 12, y: 23, width: 12, height: 100)
}
}
You can change frame in tableview(cellForRowAt) but If frame will not change its a bad because cellForRowAt run when scrolling up - down or loading cell . You can define in UITableViewCell once.

Add a custom class for this custom tableViewCell.
Create a custom tableViewCell inside the tableView through the storyboard.
Add image inside this custom tableViewCell.
Add constraints (top, bottom, leading, trailing) to this image.
This way, you won't have to do anything programmatically and the image size issues will be taken care of by these constraints.

It'd be better if you do that frame related things in storyboard or Xib files itself. if you want them with static frame.
RunTime:
You can use NSLayoutConstraints
You can change the frame in draw(_ rect: CGRect) method.

Related

UILabels and UICollectionView inside of UITableViewCell - need dynamic height

I have a custom UITableViewCell class that contains various UILabels, UIButtons, and a UITextField in the top and mid-sections, and then a UICollectionView at the bottom.
As a user enters more search terms into the UICollectionView, the collection view grows vertically, but I can't get my TableViewCell to adjust its height dynamically.
I have added constraints for every item in the TableViewCell so AutoLayout should work. I even set the heightForRowAt function to return UITableView.automaticDimension. But I'm apparently still missing something.
When the user adds/subtracts items to the collection view, how can I have the TableViewCell update its height dynamically?
I also tried the following block, but it doesn't work either.
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
collectionView.layoutIfNeeded()
collectionView.frame = CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height + titleLabel.frame.height + filterButton.frame.height + collectionView.frame.height)
return collectionView.frame.size
}

Dynamic cell height programmatically

Is it possible to make a dynamic cell height programmatically and only using a frames?
Without constraints, autolayout and external libraries!
In this custom tableView cell, i set frame for label with calculating height for text label:
override func layoutSubviews() {
super.layoutSubviews()
makeLayout()
}
func makeLayout() {
let sizeLabel = descriptionLabel.sizeThatFits(bounds.size)
descriptionLabel.frame = CGRect(x: 0, y: 0, width: bounds.width, height: sizeLabel.height)
}
I have view controller with tableView, where i implement his delegate with return automaticDimension, but it doesn't work
Yes,
You will have to return the height programmatically by implementing the
UITableViewDelegate function

Resizing NSScrollView

How can I resize NSScrollView using Swift?
class MainViewController: NSViewController {
#IBOutlet weak var scrollView: NSScrollView!
override func viewDidLoad {
scrollView.setFrameOrigin(NSPoint(x: 340, y: 537))
scrollView.setFrameSize(CGSize(width: scrollView.contentSize.width, height: 102.0))
}
}
The above code works only if there are no constraints set. But, without constraints, the ScrollView doesn't stay in place when the window is resized.
If you are trying to set the document size (scrollable area) of a NSScrollView, then all we need to do is set the document view's size.
📌 Note: This assumes there are no constraints on the document view.
scrollView.documentView?.setFrameSize(NSSize(width: 576, height: 56))
If you are trying to scroll to a specific region, see scrollToVisible(_:).
So this worked for me since my ScrollView was positioned on the top left:
scrollView.setFrameOrigin(NSPoint(x: self.view.frame.minX + 340, y: self.view.frame.maxY - 172))
scrollView.setFrameSize(CGSize(width: scrollView.contentSize.width, height: 102.0))
And the below image shows the settings used in Size Inspector to enable auto resize and also to stay to the top-left of the window.

UITextView Height Adjustment

I'm trying to create a ViewController which will hold some large text. I used UILabel but since the text can be long, it won't work. Trying to switch to UITextViewbut I'm having some problems.
I'm using ScrollView since there is a image on the top and button on the bottom. So I don't it scrolling inside the UITextView itself. I've disabled it.
I want to Text to height itself automatically according to the text length. So it can scroll with the images, buttons etc. Just like the apps Instapaper, Medium, Pocket etc.
I've tried all the code and solutions on StackOverflow, but they either didn't work or they were Objective-C.
Storyboard Structure:
View Controller -> View -> ScrollView -> Image, TextView, Button
class ViewController: UIViewController {
#IBOutlet weak var textView: UITextView!
var theContent = fromClass.text
override func viewDidLoad() {
super.viewDidLoad()
textView.text = theContent
}
}
There is few ways to change the size of the UITextView
extension String {
func bounds(approximated width: CGFloat, approximated height: CGFloat, font: UIFont) -> CGRect {
let size = CGSize(width: width, height: height)
let attribs = [NSAttributedStringKey.font: font]
return NSString(string: self).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attribs, context: nil)
}
}
approximated width = width of your UITextView I assume it is going to be static value which you won't change.
approximated height = minimal height of your UITextView.
The method going to return frame of your text input. Take from it height property and depending of how big is that change the size of the UITextView.
If it won't satisfy you, you can use another way.
func heightOfString(_ attributes: [NSAttributedKeyString : Any]) -> CGFloat {
let size = self.size(withAttributes: attributes)
return size.height
}
The same usage as from above.
Adjust your view hierarchy to the following view hierarchy :
View Controller -> View -> ScrollView -> View1 -> Image, UILabel, Button
1- give view1 top-left-bottom-right constraints and and align it center X to it's parent view
2- add UIImageView in the top of View1 and give it top-left-right constraint and a fixed height constraint.
3- add a UILabel and give it top constraint to the image and right-bottom-left constraint to it's superview.
make UILabel number of lines = 0
The content not should be scrollable.
Note that: in your case; you do not need UITextView .
If you want to use UITextView:
1- add a height constraint to the UITextView and connect an outlet to it
called: constTextViewHeight
let textViewContentHeight = textView.contentSize.height//call this line after adding the text to the textView
self.constTextViewHeight.contant = textViewContentHeight
self.view.layoutIfNeeded()

Adding UIImage to new Scroll View

I'm trying to create a simple app in Xcode that lets me press a button and it adds an image to a view. I originally had everything working until I decided to add a scroll view, as I wanted to create multiple buttons. My problem is, the created image follows down when scrolling. Im sure this is an easy fix. My code looks like this for each button:
#IBAction func btnAddColorfulLights1(_ sender: Any) {
let image: UIImage = UIImage(named: "ColorfulLights.png")!
let imageView = UIImageView(image: image)
self.view.addSubview(imageView)
imageView.tag = 10
imageView.frame = CGRect(x: 67, y: 39, width: 240, height: 338)
}
I think my problem is in,
self.view.addSubview(imageView)
Where I'm adding the image to view, where I need to be adding it to the scroll view.
This is my control viewer organization/set up:
Here's what it should look like:
Here's the problem:
I know this is probably very easy but I can't seem to find any answers to my specific problem or anything that can help me. Thank you so much!
You need to add the UIImageView containing the image of your lights to the UIView that is the contentView of the UIScrollView.
An easy way to do this is to create an #IBOutlet for the UIView in your scrollView. control-drag from the View under the ScrollView in the Document Outline to your ViewController code. Give the #IBOutlet a name such as:
#IBOutlet weak var scrollViewsView: UIView!
Then, use it to add your adornments:
#IBAction func btnAddColorfulLights1(_ sender: Any) {
let image = UIImage(named: "ColorfulLights.png")!
let imageView = UIImageView(image: image)
self.scrollViewsView.addSubview(imageView)
imageView.tag = 10
imageView.frame = CGRect(x: 67, y: 39, width: 240, height: 338)
}