UITextView add background image in ios - ios

I am trying for add image as background of UITextview
textView.backgroundColor = UIColor(patternImage: UIImage(named: "Background")!)
Output is :-
Textview not display single image ?

You need to set image in layer of textView instead of setting backgroundColor with UIColor(patternImage:). So set image this way.
textView.layer.contents = UIImage(named: "Background")!.cgImage

Related

Custom Tab Bar Background Image is not showing correctly

I'm implementing a custom Tab bar for my iOS app. Im using the following code to display a background image:
class TabNavigationMenu: UIView {
// ...
// ...
UIGraphicsBeginImageContext(self.frame.size)
UIImage(named: "tabBarbg.png")?.draw(in: self.bounds)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let image = image {
self.backgroundColor = UIColor(patternImage: image)
}
}
However, the tab bar is presented like this:
Also, when i make a view and present it anywhere else in the screen, it displays correctly. I'm using the same code there as well. Heres an example:
Any idea what the problem could be? I'm guessing the solution would also solve the faint blue line on top of the view...
You can try this to customize tab bar in ios
//Set the background color
UITabBar.appearance().backgroundColor = .red
tabBar.backgroundImage = UIImage(named: "tabBarbg.png")
//Set the item tint colors
tabBar.tintColor = .white
tabBar.unselectedItemTintColor = .lightGray

UITableViewCell: Change checkbox border colour

May I know how can I change the checkbox border colour of UITableViewCell?
You can try adding tint colour to the check mark image before assigning the image to the image view.
let borderImage = self.image?.withRenderingMode(.alwaysTemplate)
self.image = templateImage
self.tintColor = newValue
You can also add it through storyboard by selecting the image view and adding tint color to it.

How to set uiview background and uiimageview to be centered, scaled to fill and masked to bounds?

Hello I am working on feed and I am facing issues with displaying images correctly. The issue is with first two UITableViewCells, that get displayed after tableView loads. The remaining cells load correctly. The code is mention below that I am using to display the images.
Following are some screenshots of the issue.
https://ibb.co/W3rCsmB
https://ibb.co/8PZKVSh
https://ibb.co/jhwm6JH
https://ibb.co/cLyfC3C
//Code to add Image.
//Code written in configurecell()
let imageURL = URL(string: ForFeed.image_url)
let media = UIImageView(frame: mediaView.bounds)
media.downloaded(from: imageURL!)
media.contentMode = UIViewContentMode.scaleAspectFill
media.layer.masksToBounds = true
self.mediaView.insertSubview(media, at: 0)
//Code to add Background image.
//Code written in awakefromnib()
UIGraphicsBeginImageContext(self.mediaView.frame.size)
let mediaPlaceholder = UIImage(named: "nilmedia")
mediaPlaceholder?.draw(in: self.mediaView.bounds)
if let image = UIGraphicsGetImageFromCurrentImageContext(){
UIGraphicsEndImageContext()
self.mediaView.backgroundColor = UIColor(patternImage: image)
}else{
UIGraphicsEndImageContext()
debugPrint("Image not available")
}
The result should display images properly.
Just headover to storyboard and select he image view and then go to Attribute inspector
set contentView to aspect fill

Set UIImageView tintColor with multiple colors

I have this png image.
How can I change this blue color (only) to a different color in swift.
This because I might need to change to many different colors.
Even better if its possible to change the black color to white.
you can try like this:
self.img.image = self.img.image?.withRenderingMode(.alwaysTemplate)
self.img.tintColor = UIColor.black
let tableImage: UIImageView = {
let image = UIImageView()
image.image = image.withRenderingMode(.alwaysTemplate)
image.tintColor = .white
return image
}()
try this, this will change your image tintColor. I don't think you can set mutiple colors in image

UIImage Orientation Issue

Why does the background image render as it does on image 2? The portrait mode looks fine, but when I change the phone to landscape mode, it seems that the background image renders the same image multiple times to fit the entire width of the screen. Why does this happen and how can I resolve this?
Edit 1:
This is the code that I use to demonstrate this issue:
let backgroundImage = UIImage(named: "Background")
view.backgroundColor = UIColor(patternImage: backgroundImage!)
Image 1:
Image 2:
I created a UIImageView as Alladinian suggested, which helped but it didn't scale properly with the x.contentMode property (I haven't looked into why yet). You have to use the autoresizingMask property on your UIImageView declaration in order to make the background image fit properly in all content modes.
This will make it work:
let backgroundImage = UIImage(named: "Background")
let backgroundImageView = UIImageView(image: backgroundImage)
backgroundImageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
view.addSubview(backgroundImageView)

Resources