Customization of UITableViewCell not doing anything - iphone

I'm having an issue I can't figure out. I have a UITableView with a custom UITableViewCell. I've set everything up as it should and that's fine. Now I have a UIImageView in my UITableViewCell. What I'm trying to do is customize the objects inside the UITableView like this UIImageView without using storyboard. So I added the UIImageView using storyboard and then connected it as an IBOutlet to the custom UITableViewCell class file.
I'm starting of with a simple customization like:
override func awakeFromNib() {
super.awakeFromNib()
profilePictureImageView.frame = CGRect(x: 5, y: 5, width: 70, height: 70)
println("Pic Dimensions: X:\(profilePictureImageView.frame.origin.x) Y:\(profilePictureImageView.frame.origin.y) W:\(profilePictureImageView.frame.size.width) H:\(profilePictureImageView.frame.size.height)")
}
The weird thing is the println statement is showing that I've changed the dimensions of the UIImageView as expected, the problem I am having is that this is not being reflected in the UI. The weird thing is that is I do some other sort of customization in the awakeFromNib() such as rounding the UIImageView, that works. But repositioning it and changing dimensions doesn't seem to work, I don't get it. By the way I am using XCode 6.1 + Swift

Try to change the profilePictureImageView's frame in the layoutSubviews.

Related

UINavigationBar bleeding its background when PageSheet is animating

My application has a UIViewController where uses the new .pageSheet modal presentation style introduced on iOS 13.
This UIViewController has a UINavigationBar on the top and it's pinned, by constraints, at the top, leading and trailing.
I noticed that the background from this view bleeds in white while the UIViewController is animating. Take a look on this recorded GIF from a real device:
Is there anything I can do to solve this? The UIViewController and UINavigationBar were created programatically.
Maybe doing this can solve it?
override func layoutSubviews() {
super.layoutSubviews()
var originalFrame = frame
frame = originalFrame
}
I am using Swift 5.1 and Xcode 11.3. The iPhone is running iOS 13.1.3.
Probably you set the view's layer to rasterize, this is making with the .pageSheet animation not draw it correctly.
The solution, remove the code below:
navigationBar.layer.shouldRasterize = true
navigationBar.layer.rasterizationScale = UIScreen.main.scale

How add Size and Position to ImageView in UITableView Cell?

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.

Autolayout CollectionView

I am trying to use autolayout for a CollectionVIew, but when I run it in iPhone plus, a space is created between the cells.
iPhone 8 Plus
iPhone 8
I guess you already set the cell size from collection view's delegate method. One thing you need to do is to call invalidateLayout() whenever the collection view's frame changes, so it will re-render the cells.
IE:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
I solved it by setting in ViewDidLoad the size of the cell (I just set the width, but you could set the height too)
let layout = YourCollectionView.collectionViewLayout as? UICollectionViewFlowLayout
layout?.estimatedItemSize = CGSize(width: view.bounds.width / 2 , height: 114)

IBDesignable View - need ViewController at design time

I'm creating an IBDesignable component so that I can see the component render live in the Xcode storyboard. The component is simply a composition of other components. Unfortunately, one of those components requires a UIViewController to function properly. Yes, bad, but I have no control over it.
The component works fine running in the app, but does not render properly at design time because the UIViewController for that scene is not available. I have tried
marching up the responder chain looking for a UIViewController
creating an IBOutlet on the component and connecting it to the View
Controller in the storyboard
Neither of these has worked; the View Controller is always nil. Any suggestions?
A UIViewController can not be render in IBDesignable Mode.
You just can IBDesignable in a view staff.
sample:
import UIKit
#IBDesignable
class SMButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = 6
layer.borderColor = UIColor.baseInstagram.cgColor
layer.borderWidth = 0.66
titleLabel?.font = UIFont.systemFont(ofSize: 15)
titleLabel?.textColor = UIColor.baseInstagram
}
}
The sample is a round button with border.
Unfortunately none of the IBDesignable component's surrounding environment can influence the component itself.

Why does UITableViewCell.layoutSubviews() have frame.width as 600 (universal storyboard width) and not 320 (specific iPhone device)?

I have a project with a Universal storyboard (screen size: 600 x 600) containing a class that overrides UITableViewCell:
class MyTableCell: UITableViewCell
{
...
}
Rather than utilising a prototype cell, the UITableView that displays this cell registers MyTableCell as a class:
myTableView.registerClass( MyTableCell.self, forCellReuseIdentifier: "myCell" )
MyTableCell then overrides layoutSubviews:
override func layoutSubviews ()
{
super.layoutSubviews()
let width = CGRectGetWidth( frame )
}
Great! However, the width that is returned is the universal 600, and not the device-specific 320 that I need.
What am I doing wrong please? Thank you.
layoutSubviews() will get called at different times in the cell's lifecycle, and can have different values as it gets moved around. When you first get it off of the storyboard it will have the width you see in the storyboard. After it gets inserted in the table view it will have the same width as the table view.
Normally this isn't a problem. It will get called with the wrong size once and then again with the correct size. But it may seem confusing in the debugger.
If you never see it change to the 320 you expect, that probably means that your table view isn't configured properly for auto layout in the storyboard, and is actually drawing at 600 pixels wide at runtime.
Put a breakpoint on that method, and try entering this into the debug console to see what the table view size is. This should return lots of information about the table view, including its size.
po superview