Resize My TableView Background - swift

I have changed my TableView background by using this code
tableView.backgroundView = UIImageView(image: UIImage(named: "wall"))
I would like to resize the background programmatically
thank you

Have you tried changing the backgroundView's frame?
var tableView = UITableView()
tableView.backgroundView?.frame = CGRect(x: yourX, y: yourY, width: yourWidth, height: yourHeight)
EDIT
For altering the content mode for the image, you can do something like this:
var imageView = UIImageView(image: UIImage(named: "image"))
imageView.contentMode = .scaleAspectFill
or
imageView.contentMode = .scaleToFill
or
imageView.contentMode = .scaleAspectFit
You can see which one works best for you. There are also a few more, so you can choose the best one.

Related

UIImage not resizing according to UIImageView size width and height

I am a Swift newbie and I am trying to add an UIImageView on the right of UITextField using Storyboard and #IBInspectable annotation.
However, when I define my UIImageView with specific size and add the UIImage, the UIImage is not resizing according to UIImageView size.
Here's my code:
#IBInspectable var rightImage: UIImage? {
didSet {
guard let image = rightImage else {
return
}
rightViewMode = .always
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = image
imageView.contentMode = .scaleAspectFit
rightView = imageView
and here's the result on the screen:
Big big icon
If you can tell me what is wrong with my code, do not hesitate.
Thanks.
I use constraints based on Matt’s suggestion in the comment, and it's working now:
let constraints = [
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
]
NSLayoutConstraint.activate(constraints)

Cant get rid of grey colour behind TabBar in Swift

I put a background image to my view, so the full view should have it.
let background = UIImage(named: "background")
var imageView : UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIView.ContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.image = background
imageView.center = view.center
view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
Also I have a barTintColor for my tabBar:
tabBar.barTintColor = UIColor.blue
The problem is when my TabBar is transparent for some percent, I can see behind the grey color(probably the default color) instead of my view's background image...
Debug view hierarchy:
Try the following
UITabBar.appearance().isTranslucent = false

Navigation Bar logo title not centered

I've implemented a logo as the navigation bar title, using the code below:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "nav_bar_icon_gradient.png")
imageView.image = image
self.navigationItem.titleView = imageView
It works however the item is fully centred on the navigation bar (see screenshot), I'm aware it could be because of the navigation bar item on the right. Does anyone know how to fix this?

How do I add a background to a scroll view and have it repeat to the end?

I am making a roster on a scroll view with every slide being someone different. I was wondering if there is a way to set a background and have it repeat to the end as it cuts off at a certain person
Currently, I am copy and pasting the background and dragging it to match
for i in 0..
let label = UILabel(frame: CGRect(x:xPosition+110, y: 350, width: 200, height: 200))
label.text = names[i]
label.font = UIFont.systemFont(ofSize: 35, weight: UIFont.Weight.bold)
label.adjustsFontSizeToFitWidth = true
label.textColor = UIColor.black
let imageView = UIImageView()
imageView.image = UIImage(named: names[i])
imageView.contentMode = .scaleAspectFit
imageView.frame = CGRect(x: xPosition-110, y: 150, width: 650, height: 250)
let descriptionM = UITextView(frame: CGRect(x: xPosition+65, y: 525, width: 300, height: 250))
descriptionM.text = desc[i]
descriptionM.textColor = UIColor.black
descriptionM.backgroundColor = UIColor.clear
descriptionM.contentMode = .scaleAspectFill
descriptionM.font = UIFont.systemFont(ofSize: 20)
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
scrollView.addSubview(label)
scrollView.addSubview(imageView)
scrollView.addSubview(descriptionM)
}
Results would be the photo to repeat to the end and past.
One way is to turn the background image into a resizable image with zero edge insets and the default (tiling) resizing mode. Set your image view's contentMode to .scaleToFill.
let image = UIImage(named: "background")!.resizableImage(withCapInsets: .zero)
imageView.contentMode = .scaleToFill
imageView.image = image
Then make sure your imageView is at least as big as your scroll view's contentSize.

Logo in Navigation Bar Swift not showing

I'm trying to add a logo to my page's navigation bar, however it doesn't appear when I added this code to my viewDidLoad():
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Does anybody know why this might not be working? I checked that the image name is correct. This is my storyboard if it helps:
So it worked when I made an outlet,
#IBOutlet weak var navItem: UINavigationItem!
then put the following code in my viewDidAppear:
let logo = UIImage(named: "Dog_Log_Icon_4"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
navItem.titleView = imageView
not sure why I had to do that..
The reason is because you forgot set the position for the imageView.
You can change your code like this:
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)
imageView.image = logo
self.navigationItem.titleView = imageView
You have not set the frame of the image view. I have set frame of the image view and it works fine.
if let logo = UIImage(named: "download"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.navigationItem.titleView = imageView
}else{
print("Image not found...")
}