How to remove search bar background? - ios

I want to remove search bar background and I have tried the following code in custom search bar subclass:
-(void)didAddSubview:(UIView *)subview
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
subview.alpha=0;
}
}
every thing ok but when click on search bar or cancel search the background color set to black
any one knows how to remove this background color?

searchbar.barStyle = UIBarStyleBlackTranslucent;
Hope it helps you

I used this code in one of my applications to remove the background and added my own background image
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
UIImageView *searchImage=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 319.0, 44.0)];
[searchImage setImage:[UIImage imageNamed:#"searchbg.png"]];
[searchBar insertSubview:searchImage atIndex:1];
This may help you.

Related

UIButton fixed background color when clicked in Objective C

I have been trying to make the buttons' background color fixed on a .xib. Like, default background is white but when the button is clicked it is going to be blue or black and it will remain that way until another button is clicked.
I did most of the combinations with UIControlState, background color and image but nothing seems working. Background color changes for a sec and goes to default after a sec.
This is one of the combinations I tried
[self.theButton addTarget:self action:#selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
-(void) slideAction:(UIButton *)myButton
{
[self.theButton setImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateNormal];
}
Function works but it does not update the color. I don't know if it the problem is related to .xib.
I appreciate of you can help me about the problem.
Thanks.
on that button click put following code
-(void) slideAction:(UIButton *)myButton
{
[self.theButton setBackgroundColor:[UIColor blueColor]];
}
and put that code for another button click
-(void) AnotherButtonAction:(UIButton *)myButton
{
[self.theButton setBackgroundColor:[UIColor whiteColor]];
}
Please follow this method:
- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state;
Try below code
-(void)code:(UIButton *)sender
{
for (UIView* subView in self.view.subviews)
{
if ([subView isMemberOfClass:[UIButton class]])
{
[subView setBackgroundColor:[UIColor greenColor]];
[sender setBackgroundColor:[UIColor blackColor]];
}
}
}
-(IBAction)firstBtnAction:(id)sender
{
[self code:sender];
}
-(IBAction)secondBtnAction:(id)sender
{
[self code:sender];
}
-(IBAction)thirdBtnAction:(id)sender
{
[self code:sender];
}
-(IBAction)fourthBtnAction:(id)sender
{
[self code:sender];
}
This is because backgroundColor is a property of UIView, as you know UIButton is a subclass of UIView.
This is important because then it is easier to understand why this property do not have States: A view do not have states. For some reason Apple decided to do dot extended the property behaviour for the buttons.
The method created in iOS to change the backgroundColor of UIButtons depending on the State is: setBackgroundImage: forState:. I know, that means you need an image for every state. A waste of space and memory.
To solve that problem I created a Category to handle backgroundColor States with UIButtons:
ButtonBackgroundColor-iOS
You can install the category as a pod.
Or do it old school way: drag into your project the folder /ButtonBackgroundColor-iOS. That’s all.
Easy to use with Objective-C
#property (nonatomic, strong) UIButton *myButton;
...
[self.myButton bbc_backgroundColorNormal:[UIColor redColor]
backgroundColorSelected:[UIColor blueColor]];
Even more easy to use with Swift:
import ButtonBackgroundColor
...
let myButton:UIButton = UIButton(type:.Custom)
myButton.bbc_backgroundColorNormal(UIColor.redColor(), backgroundColorSelected: UIColor.blueColor())
I recommend you import the pod with:
platform :ios, '8.0'
use_frameworks!
pod 'ButtonBackgroundColor', '~> 1.0'
Using use_frameworks! in your Podfile makes easier to use your pods with Swift and objective-C.
IMPORTANT
I also wrote a Blog Post with more information.

Cannot set UISearchBar's translucent property to NO

I am trying to set the barTintColor of a UISearchBar without translucency. However, setting the translucent property doesn't seem to do anything. I have reproduced the issue in a bare-bones Xcode project here.
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.barTintColor = [UIColor redColor];
The red color above is not the same as [UIColor redColor] in UIViews that are not translucent. I know about the workaround involving setting a background image on the search bar, but the above code should work as well.
I downloaded your code and find solution for, add one method name is removeUISearchBarBackgroundInViewHierarchy and set searchBar.backgroundColor as redColor.
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchDisplayController.searchBar.translucent = NO;
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = [UIColor redColor];
}
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
Please set image in search bar like this it will solve your problem.
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:#"red"]];
Thanks.
Maybe too late for the party,
However I've done a very simple and nifty extension for Swift 3 that allows you to use the translucent property without any trouble.
It involves no use of private API or dangerous walk-through within the object.
You can download it from this Github repository.

How do I make the area on the top of a UISearchBar (when on a UITableView) in iOS 7 transparent?

Here is a picture describing the problem. Notice that the area above the UISearchBar does not have the background as below it. I don't want the gray color, I want the dark maroon background when pulling down on the UITableView.
Everything I know of is set to [UIColor clearColor], including backgrounds of views, etc. It works fine in iOS 6 but not iOS 7!
I've also attached a sample project so someone can take a look and see what I am doing wrong here?
Click here to download my sample project
Maybe I am just missing something stupid?
Thanks!
Two things you could do: 1) add an explicit clear background view to the UITableView. Or, 2) set your UIImageView to be the background view of the UITableView.
Either of these work. Here's the code that makes your sample work:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
backgroundImageView.image = [UIImage imageNamed:#"Film_bg.png"];
[self.view addSubview:backgroundImageView];
[self.view sendSubviewToBack:backgroundImageView];
// solution 1
// self.tableView.backgroundView = [UIView new];
// self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
// solution 2
self.tableView.backgroundView = backgroundImageView;
}

A UIButton above a UIPickerView

I have a UIPickerView which works correctly, now I want to add a button above it so that I can dismiss it.
and here is my code where I initiate a UIPickerView as well as its dismiss button:
- (UIPickerView *)creatPickerView {
UIPickerView *tempPickerView = [[[UIPickerView alloc]
initWithFrame:CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
tempPickerView.showsSelectionIndicator = YES;
tempPickerView.delegate = self;
tempPickerView.dataSource = self;
UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(270, -32, 50, 32)];
[pickerButton setBackgroundImage:[UIImage imageNamed:#"hidePicker.png"]
forState:UIControlStateNormal];
[pickerButton addTarget:self action:#selector(hidePicker)
forControlEvents:UIControlEventTouchUpInside];
[tempPickerView addSubview:pickerButton];
[pickerButton release];
[self.view addSubview:tempPickerView];
return tempPickerView;
}
and it works well on my iPhone 4.3 Simulator, like this:
apparently there is a button on the upper right of the pickerView,
problem is, when I run the app in my device - a 5.0.1 iPhone4 and a 4.2.1 iTouch, the button is missed like it has never been added to the pickerView.
Can anyone help me with this?
Thanks a lot and a lot!
I found the reason, it seems the png has some problem,
after I change another png, it comes up in the screen!
but the real problem is that I place the button outside of the pickerView which results in the button's untouchableness.
But anyway the pickure is only a small problem.

Custom tab bar background image - in iOS 4.x

I have made a tab bar iOS project, when I received the request to change the tab bar's background image to a custom image. The project is developed for iOS 4.x, so the iOS5's
[tabBar setTabBarBackgroundImage:[UIImage imageNamed:#"custom.jpg"]] does not work. Can you suggest me some simple solutions (if there is any possibility, I would not like to change the entire project)?
Edit:
Only three lines of code can resolve all:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"customImage.png"]];
[self.tabBarController.tabBar insertSubview:imageView atIndex:0];
[imageView release];
A possible solution would be to put an UIView with your background image exactly behind the UITabBar. Then lower the opacity of your tabbar to 0.5 so you can see the background-image coming through.
UIView *bckgrndView = [[UIView alloc] initWithFrame:CGRectMake(tabbar.frame.coords.x, tabbar.frame.coords.y, tabbar.frame.size.width, tabbar.frame.size.height)];
[bckgrndView setBackgroundImage:[UIImage imageNamed:#"custom.jpg"]];
[tabbar.superView insertSubView:bckgrndView belowSubview:tabbar];
tabbar.alpha = 0.5;
[bckgrndView release];
Sorry if my code contains some errors. I tried doing this by heart... But you'll catch the drift.
I have answered similar kind of question here. Hope it will help.
Check out NGTabBarController, an open source tab bar replacement with customizable background image.

Resources