Why does opaque style not work on SearchBar? - ios

How can I make the search bar tint color full blue?
I've tried:
searchBar.opaque = true
also
searchBar.translucent = false
But it does not work. Why?

From the documentation on .opaque
var opaque: Bool { get set }
A Boolean value that determines whether the view is opaque.
...
You only need to set a value for the
opaque property for subclasses of UIView that draw their own content
using the drawRect: method. The opaque property has no effect for
system provided classes such as UIButton, UILabel, UITableViewCell,
etc.
Hence, the .opaque properties will have no effect on a native UISearchBar; therefor, in you're example above, searchBar.opaque has no effect.
Regarding the .translucent property, the documentation states:
var translucent: Bool { get set }
A Boolean value that indicates whether the search bar is translucent
(true) or not (false).
The default value is true. If the search bar has a custom background
image, the default is true if any pixel of the image has an alpha
value of less than 1.0, and false otherwise.
If you set this property to true on a search bar with an opaque custom
background image, the search bar will apply a system opacity less than
1.0 to the image.
If you set this property to false on a search bar with a translucent
custom background image, the search bar provides an opaque background
for the image using black if the search bar has UIBarStyleBlack style,
white if the search bar has UIBarStyleDefault, or the search bar’s
barTintColor if a custom value is defined.
Hence, to achieve a transparent background for your search bar, you need to set also a background image for it, which has been described previously in the following SO thread
Can't change search bar tint color to be transparent in iOS 8
Using Mike:s answer in the linked thread (Obj-C), we can adapt to swift according to:
searchBar.barTintColor = UIColor.clearColor()
searchBar.backgroundImage = UIImage()
searchBar.translucent = false
This should achieve a transparent search bar.

Related

while using ModalBottomSheetLayout, how to make background and status bar transparent in Jetpack compose

I am new to jetpack. My app is having ModalBottomSheetLayout in MainScreen. Now when I click on button of MainScreen, it shows BottomSheet. When bottom sheet is open, the background is transparent but status bar is not. How to make it transparent?
So far, you can use the System UI Controller in the accompanist library to control the statusBar Color and navigationBar color
implementation "com.google.accompanist:accompanist-systemuicontroller:0.18.0"
// Remember a SystemUiController
val systemUiController = rememberSystemUiController()
val useDarkIcons = MaterialTheme.colors.isLight
SideEffect {
// Update all of the system bar colors to be transparent, and use
// dark icons if we're in light theme
systemUiController.setSystemBarsColor(
color = Color.Transparent,
darkIcons = useDarkIcons
)
// setStatusBarsColor() and setNavigationBarsColor() also exist
}

Bar section in iOS is extended unnecessarily

In the image shared, the orange section is the bar section , which is having unnecessary height, I am not able to resolve this issue by myself.
the views are like this
Parent Controller = View Controller
Child views = green view, black tableview
Please help to correct the height of the orange bar.
Seems like you have enabled prefersLargeTitles.
Make it false in your viewWillAppear()
self.navigationController?.navigationBar.prefersLargeTitles = false
You can also disable it from the storyboard.
Select your Navigation Controller -> Navigation Bar -> Uncheck prefers large titles
it seems like you're using the largeTitles on the navigationBar,
var prefersLargeTitles: Bool { get set }
When this property is set to true, the navigation bar allows the
title to be displayed out-of-line and using a larger font. The
navigation item used to build the bar must specify whether it wants
its title displayed in the large or small format. Use the
largeTitleDisplayMode property to configure the title's appearance.
When the property is set to false, the navigation bar displays the
title inline with the other bar button items.
try to disable it by:
navigationController?.navigationBar.prefersLargeTitles = false
or you can do this as well:
navigationItem.largeTitleDisplayMode = .never
hope this helps:)
https://developer.apple.com/documentation/uikit/uinavigationbar/2908999-preferslargetitles

Changing tintColor to default (blue) in NavBar

I can change the tintColor for my App (everywhere) in a setting pane.
When I change the tintColor, and after that try to return to the default tint color, some buttons in NavBar don't return to blue, how can I handle this?
To verify:
- create a new project Master/Detail
- In the detail view: add Two buttons, named: "Red Interface" and "Blue interface"
- In DetailViewController, add the actions
#IBAction func tapRed(sender: AnyObject) {
view.window!.tintColor = UIColor.redColor()
}
#IBAction func tapBlue(sender: AnyObject) {
view.window!.tintColor = nil
}
Now run the application, create a timestamp, go to detail, tap redInterface, then blue interface
That's OK in the Detail View, but when you return to Master, the "+" button item is red, not blue.
I can fix the problem by setting a real blue color instead of nil, but that's not a long term solution, if Apple change the default tintColor.
Is this a bug? Is there something I can do to bypass this problem?
A quick workaround would be to fetch the default tint color once on the initial app load and store it somewhere (i.e. in the user defaults)
From Apple's iOS 7 UI Transition Guide (Specifically under the Using Tint Color section).
By default, a view’s tint color is nil, which means that the view uses its parent’s tint. It also means that when you ask a view for its tint color, it always returns a color value, even if you haven’t set one.
That's a good workaround!
In fact, I don't even need to store the color.
If I change to
#IBAction func tapBlue(sender: AnyObject) {
view.window!.tintColor = UIButton(type:UIButtonType.System).titleColorForState(.Normal)
}
That gives me the correct blue color, even if I changed the tintColor (probably because I don't address a button in the view hierarchy)

How can I change the toolbar color in Swift

I'm working on an app and I'm not sure how to change the color of the bottom toolbar in swift. I would like to have it as a custom image to match my navigation bar. Does anyone have a code for that or just to change the color. Thank you
Change background color:
self.toolbar.barTintColor = UIColor.redColor()
Change background image:
self.toolbar.setBackgroundImage(UIImage(named: "BackgroundImage"), forToolbarPosition: .Bottom, barMetrics: .Default)
NOTE: All my answers are in Swift 3
In order to change the background color of the toolbar do the following:
self.toolbar.isTranslucent = false
self.toolbar.barTintColor = UIColor.red
This code is similar to #Bannings answer, however his answer is missing the isTranslucent property, which must be set to false first. Otherwise, it won't work.
In order to change the background image do the same as #Bannings suggested:
self.toolbar.setBackgroundImage(UIImage(named: "BackgroundImage"), forToolbarPosition: .bottom, barMetrics: .default)
It should be stated here that the background image is visible due the fact that the isTranslucent property is set to true by default (assuming the background image is not opaque).
It always helps to read the Apple's description on the isTranslucent property for toolbars:
A Boolean value that indicates whether the toolbar is translucent (true) or not (false).
The default value is true. If the toolbar has a custom background image, the default is true if any pixel of the image has an alpha value of less than 1.0, and false otherwise.
If you set this property to true on a toolbar with an opaque custom background image, the toolbar will apply a system opacity less than 1.0 to the image.
If you set this property to false on a toolbar with a translucent custom background image, the toolbar provides an opaque background for the image using black if the toolbar has black style, white if the toolbar has default, or the toolbar’s barTintColor if a custom value is defined.
If the tool bar is anchored with navigation controller, go to IB to change the color.
1 . go to your navigation controller,
2 . show "document outline"
3 . select "Toolbar" under the navigation controller (usually, it is below Navigation bar)
one the right side, choose your prefered "Bar Tint" / "Translucent"
This also work on swift 3
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.barTintColor = UIColor.red
Your UIBarStyle should be default.
This also works on swift 3:
UINavigationBar.appearance().barTintColor = UIColor.red

Change colour of dark grey highlight when holding down custom UIButton?

I have a custom UIButton which is a cloud, transparent black and white .png file, with no down state, just one image. When tapping and holding the finger over it, it turns dark grey. I'm trying to change that dark grey to something a little less oppressive. The button is out in the open in a view, not in a tab bar, tool bar, or navigation controller.
I've already tried setting tintColor (which the documentation helpfully informs me is only suitable for 'some' types of buttons, which no indication as to which).
I've also tried changing everything I can find in Interface Builder relating to highlight colours, default states, etc. Nothing has made a difference at all.
I've even tried setting the button's own image for its UIControlStateHighlighted state, but even this causes the dark grey overlay to appear when I hold my finger over it.
How can I change that colour? I've looked at numerous other issues here on SO and have been unable to find a solution that works for me. Any help would be greatly appreciated!
EDIT: I Solved the problem using a category of UIImage which adds a method that uses CoreGraphics to apply a tint to a provided UIImage. I then set THAT image as the highlight, and all is well. Seems a lot of hoop-la to change a colour Apple should've let us change, but c'est la vie.
You said you set a custom image for the UIControlStateHighlighted state. This should disable the default behaviour.
If you still have problems you can disable this effect by setting the adjustsImageWhenHighlighted property to NO and use whatever custom effect you want.
If adjustsImageWhenHighlighted = NO is not working,
set Button-Type to Custom (IB or programmatically).
Default Button-Type: System, changes behavior of highlighted button.
Swift 3:
myButton.adjustsImageWhenHighlighted = false
I was having a similar issue with a custom UIButton when the button was highlighting in grey every time it was pressed. I solved that problem by subclassing UIButton and in the implementation I overrode a single method, (void)setHighlighted: method and kept it empty:
- (void)setHighlighted:(BOOL)highlighted
{
// Leave empty to prevent super from doing whatever
// that it is doing to show the grey highlight.
}
That stopped any type of highlighting as I was not doing anything in the method. It's a better approach if all that you're trying to do is remove any highlighting effect.
So in your code, create a subclass of UIButton, override the setHighlighted method, and then make your custom button a subclass of this custom class.
You can write a custom button that does it
class ActionButton: UIButton {
var originalBackgroundColor: UIColor!
override var backgroundColor: UIColor? {
didSet {
if originalBackgroundColor == nil {
originalBackgroundColor = backgroundColor
}
}
}
override var isHighlighted: Bool {
didSet {
guard let originalBackgroundColor = originalBackgroundColor else {
return
}
backgroundColor = isHighlighted ? originalBackgroundColor.darken() : originalBackgroundColor
}
}

Resources