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
Related
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
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.
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)
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
With iOS 7, it's now pretty easy to add a blur to UINavigationBar, even with a BarTint, see http://blog.ashleynh.me/frosted-uinavigationbar/ and this example image:
However, there's a border at the bottom. How can I get rid of the border to look more like this?
UPDATE:
I took Danny and Shali's code, and here are the results. As you can see, the border doesn't show any more but there is no blur.
let navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationBarAppearance.shadowImage = UIImage()
navigationBarAppearance.translucent = true
and here's the Inspector screenshot:
I also tried:
let navigationBarAppearance = UINavigationBar.appearance()
let clearImage = UIImage.imageWithColor(UIColor.clearColor())
navigationBarAppearance.setBackgroundImage(clearImage, forBarMetrics: UIBarMetrics.Default)
navigationBarAppearance.shadowImage = clearImage
navigationBarAppearance.translucent = true
Same result, but the Inspector is a little different:
Apple Documents: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationBar_Class/index.html#//apple_ref/occ/instp/UINavigationBar/shadowImage
The default value is nil, which corresponds to the default shadow
image. When non-nil, this property represents a custom shadow image to
show instead of the default. For a custom shadow image to be shown, a
custom background image must also be set with the
setBackgroundImage:forBarMetrics: method. If the default background
image is used, then the default shadow image will be used regardless
of the value of this property.
So basically you need to set background image before setting shadowImage to make it work.
Edit
Image generated from color (Swift) as background Navigation. Not sure if your blur function will still work when you change backgroundImage for Navigation bar. That would be a different problem.
class func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0, 0, 1, 1)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
color.setFill()
UIRectFill(rect)
var image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
Have you tried navigationBar.shadowImage = [UIImage new]; ?