How to set clear UIWebView Background? - iphone

I am trying to set the clear background of web view, but i could not found anything for this issue, so please help me, so How will i remove gradient background and make UIWebview transparent?
Thanks

To make UIWebView Background transparent you may use
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
If you also want to remove shadows create Category for UIWebView:
UIWebView+ShadowRemoval.m
#import "UIWebView+ShadowRemoval.h"
#implementation UIWebView (ShadowRemoval)
-(void)removeShadows
{
for(UIView *view in [self subviews] ) {
if( [view isKindOfClass:[UIScrollView class]] ) {
for( UIView *innerView in [view subviews] ) {
if( [innerView isKindOfClass:[UIImageView class]] ) {
innerView.hidden = YES;
}
}
}
}
}
#end

Please try this snippet code with your web view
myWebView.opaque = NO;
[myWebView setBackgroundColor:[UIColor clearColor]];

Related

Xcode: How to change all fonts at once in an app?

I was wondering if it was possible to change the fonts on about 100 different viewcontrollers all at once? It would be a lot eisier than going through one by one and changing them. Any ideas? Thank you!
The user interface files (*.xib) are plain text and you can load them into an editor.
In Xcode4 on the left pane you can right-click > open as > source.
This will give you the XML source any you can find/replace there.
Warning: doing something wrong may render the whole file useless, so unless you have source control anyway, make copies of the XIB before attempting changes.
you cant change all the fonts at once....
But i have find one more varient that will help you...
I have made some recursive functions thy can help you..
follow following steps..
First create a class(BaseViewController) extended from UIViewController like in BaseViewController.h file
#interface BaseViewController : UIViewController
And in BaseViewController.m file write following code.
- (void)viewDidLoad
{
[super viewDidLoad];
[self changeFontsOfViewController];
}
-(void)changeFontsOfViewController
{
UIViewController * vv = [self viewControllerOfView:self.view];
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([vv class]) owner:vv options:nil];
for (id object in objects)
{
[self changeFontOfView:object];
}
}
-(void)changeFontOfView:(UIView *)aView
{
for (UIView *vv in [aView subviews])
{
if ([vv isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)vv;
CGFloat fontSize = btn.titleLabel.font.pointSize;
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:fontSize];
}
else if ([vv isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)vv;
CGFloat fontSize = lbl.font.pointSize;
[lbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
}
else if ([vv isKindOfClass:[UITextView class]])
{
UITextView *txt = (UITextView *)vv;
CGFloat fontSize = txt.font.pointSize;
[txt setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
}
else if ([vv isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField *)vv;
CGFloat fontSize = txt.font.pointSize;
[txt setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
}
else if ([vv isKindOfClass:[UIView class]]||[vv isKindOfClass:[UIScrollView class]])
{
if (aView.subviews.count == 0)return;
[self changeFontOfView:vv];
}
}
}
Now your every viewController(RootViewController) will be extended from BaseViewController class like in RootViewController.h..
#import "BaseViewController.h"
#interface RootViewController : BaseViewController
{
}
And make sure that you have written following in your .m file of your UIViewController(RootViewController.m)
- (void)viewDidLoad
{
[super viewDidLoad];
}
Please follow above steps carefully you will rock.......
How about this little recursive method on UIView?
#implementation UIView (JPCSetFont)
- (void)jpc_setAllFonts:(UIFont *)font
{
if ([self respondsToSelector:#selector(setFont:)]) {
UIFont *oldFont = [self valueForKey:#"font"];
UIFont *newFont = [font fontWithSize:oldFont.pointSize];
[self setValue:newFont forKey:#"font"];
}
for (UIView *subview in self.subviews) {
[subview jpc_setAllFonts:font];
}
}
#end
Code by John Cromartie is a good stuff. Simple and laconic.
Just keep in mind some fonts are bold or italic. So you probably have to pass 2 (or 3) params: for regular font (as it is now), for bold font and for the italic one. Besides you have to detect if the current font is bold, etc. So there is a bit enhanced piece of code below.
However, it can work wrong on iOS7 because user can set his own fonts and, as a result, weight / style detection won't work properly.
#implementation UIView (JPCSetFont)
- (void)jpc_setAllFonts:(UIFont*)regular bold:(UIFont*)bold
{
if ([self respondsToSelector:#selector(setFont:)]) {
UIFont *oldFont = [self valueForKey:#"font"];
UIFont *newFont;
// for iOS6
NSRange isBold = [[oldFont fontName] rangeOfString:#"Bold" options:NSCaseInsensitiveSearch];
// for iOS7 (is device owner didn't change it!)
NSRange isMedium = [[oldFont fontName] rangeOfString:#"MediumP4" options:NSCaseInsensitiveSearch];
if (isBold.location==NSNotFound && isMedium.location==NSNotFound) {
newFont = [regular fontWithSize:oldFont.pointSize];
} else {
newFont = [bold fontWithSize:oldFont.pointSize];
}
// TODO: there are italic fonts also though
[self setValue:newFont forKey:#"font"];
}
for (UIView *subview in self.subviews) {
[subview jpc_setAllFonts:regular bold:bold];
}
}
#end
https://github.com/iutinvg/ZZLib/blob/master/ZZLib/UIView%2BZZFontSetter.h
https://github.com/iutinvg/ZZLib/blob/master/ZZLib/UIView%2BZZFontSetter.m

How to change color of Search Bar?

I've implemented a Search Bar and I want to change color of Search Bar. How can I do that?
I've tried with:
self.mySearchBar.backgroundColor = [UIColor redColor];
but with no success.
UPDATE (Solved):
I had to remove the default background color before I defined background color with the following code.
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
... so code would be next:
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
self.mySearchBar.backgroundColor = [UIColor redColor];
Try:
self.mySearchBar.barTintColor = [UIColor redColor];
self.mySearchBar.backgroundColor = [UIColor redColor];
won't do anything,
but
self.mySearchBar.tintColor = [UIColor redColor];
will!
self.mySearchBar.backgroundVolor = [UIColor redColor];
"Volor" or "Color
self.mySearchBar.backgroundColor = [UIColor redColor];
Also if you want to tint it:
According to the documentation : https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchBar_Class/Reference/Reference.html, there is a tintColor property on the UISearchBar class.
In the TableSearch example: https://developer.apple.com/library/ios/#samplecode/TableSearch/ the search bar is defined and loaded in the MainView.xib. If you want to change its tintColor or style, just do it in the xib and it will be loaded into the application.
BUT THE ONLY REAL WAY to change background color is to override it, look at the answer from the following question
UISearchBar clear background color or set background image [iphone sdk]
I'm not sure if it has changed since iOS7, but it seems that the search bar in the search display controller requires an extra loop to properly remove the UISearchBarBackground view.
I created a recursive method to properly clean the search bar.
- (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];
}
}
}
You can simply send your search bar to the method and change the color afterward, a bit like the suggested answers above.
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
I had to set UISearchBar's searchBarStyle to .default, otherwise nothing works.
searchBar.barTintColor = UIColor.redColor()
Try this code it works fine for me.
for( UIView* v in [parent subviews] ) {
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
}
If you have issues with top and bottom lines try to remove the UISearchBarBackground:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
for (UIView *subsubview in subview.subviews) {
if ([subsubview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subsubview removeFromSuperview];
break;
}
}
}
self.searchBar.barTintColor = [UIColor redColor];
self.searchBar.backgroundColor = [UIColor redColor];
Depends what exactly are you going to change
searchBar.barTintColor
searchBar.tintColor
Other items you are able customise thought some extra code

how to set background color as a clear color of searchbar

I want to make searchbar's background as a clear or looklike navigationbar's color
currently showing
how to make the backgroung clear of serach bar?
Beacause UI SearchBar has got its own background view, which is of black color, if we remove that view then UISearchbar's background will become clear :
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
//Search bar(using background image)
UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)];
UIImageView* iview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"blue.png"]];
iview.frame = CGRectMake(0, 0, 200, 44);
[bar insertSubview:iview atIndex:1];
[iview release];
[self.view addSubview:bar];
[bar release];
OR(using color)
//Search bar
UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)];
[[[bar subviews] objectAtIndex:0] setAlpha:0.0];
bar.tintColor = [UIColor colorWithRed:.376f green:.386f blue:.452f alpha:1.0];
bar.clipsToBounds = YES;
[self.view addSubview:bar];
[bar release];
Set the background image for the UISearchBar as follows.
[searchBar setBackgroundImage:[UIImage new]];
Now the UISearchBar background color will disappear. Will also work in iOS 7.1
For Clear the background color (If you want images set then use comment code)
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
// if You want set iamge then use comment code
/* UIView *backgroundView = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"searchBar"]];
[self.searchBar insertSubview:backgroundView aboveSubview:subview];*/
[subview removeFromSuperview];
}
}
For clearing search bar's background view :
[[[self.searchBar subviews] objectAtIndex:0] setHidden:YES];
for (id subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
}
}
I found in iOS8 that this could worked better for reducing a UISearchBar to just a text field. This will allow you to get rid of the background color.
for (UIView *subView in self.searchBar.subviews) {
for (UIView *secondLevelSubview in subView.subviews) {
if (![secondLevelSubview isKindOfClass:NSClassFromString(#"UISearchBarTextField")]) {
[secondLevelSubview removeFromSuperview];
}
}
}
Try with this line :
[[ [searchBar subviews] objectAtIndex:0] removeFromSuperview];
[[ [searchBar subviews] objectAtIndex:0] setBackgroundColor:[UIColor yellowColor]];
Surely help you out.

Stop Scrolling in UIWebview

I need to know how to stop scrolling in a UIwebview in xcode 4?
[[[webView subviews] lastobject] setScrollingEnabled:NO];
The above code does not work because "NSarray" for instance message does not declare a method with selector 'lastobject'. Why is this or is there new code I am unaware of to disable scrolling? Thanks.
UIView* row = nil;
for(row in webView.subviews){
if([row isKindOfClass:[UIScrollView class] ]){
UIScrollView* scrollRow = (UIScrollView*) row;
scrollRow.scrollEnabled = NO;
scrollRow.bounces = NO;
scrollRow.backgroundColor=[UIColor clearColor];
}
}
your code is trying to make assumptions about the order of the subviews that Apple is defining..
Note that with iOS 5 you have direct access to the scroll view.
So if you wanted to maintain compatibility you could do this:
UIScrollView *scrollView = nil;
if ([webView respondsToSelector:#selector(scrollView)]) { //iOS 5+
scrollView = webView.scrollView;
} else { //iOS 4-
for(UIView *view in webView.subviews){
if([view isKindOfClass:[UIScrollView class] ]){
scrollView = (UIScrollView *) view;
break;
}
}
}
scrollView.scrollEnabled = NO;
scrollView.bounces = NO;
scrollView.backgroundColor=[UIColor clearColor];
Simply add this line to viewDidLoad:
myWebView.scrollView.scrollEnabled = NO;
I would set the webview's UserInteractionEnabled property to NO so that it cannot be scrolled.

Customizing search bar in iPhone application Development

In my application I have to add a search bar at the head of the tableview. I am able to add the searchbar but problem is without adding default search bar of ios can i add my customize search bar?? I am giving an image to see what types of search bar will be there...
you can subclass the UISearchBar and override the layoutSubviews method :
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:#"yourImage.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
Also you can :
//to clear searchbar backgraound
- (void) clearSearchBarBg
{
for (UIView *subview in theSearchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
}
//display showSearchButtonInitially in a keyboard
- (void)showSearchButtonInitially
{
UIView * subview;
NSArray * subviews = [theSearchBar subviews];
for(subview in subviews)
{
if( [subview isKindOfClass:[UITextField class]] )
{
NSLog(#"setEnablesReturnKeyAutomatically");
[((UITextField*)subview) setEnablesReturnKeyAutomatically:NO];
((UITextField*)subview).delegate=self;
[((UITextField*)subview) setEnabled:TRUE];
((UITextField*)subview).borderStyle = UITextBorderStyleNone;
break;
}
}
}
Look for Apple DOC for UISearchBar
You have bunch of methods there to get whatever you want
You can get UITextView Inside the search bar by
UITextField *textField = [searchBar.subviews objectAtIndex:2];
if ([textField isKindOfClass:[UITextField class]]) {
//Do your customization
}
Again look for AppleDoc for UITextField. You have bunch of methods for that also.
Yeah definitely. You can make your custom search bar (which is a sub-class of UIView) and add it as subview to the tableHeaderView.
[[searchBarDesign.subviews objectAtIndex:0] removeFromSuperview];
here searchBarDesign is my searchBar name.
I think it's better just set all properties of UISearchBar when it is loaded.
#interface MySearchBar : UISearchBar
#end
#implementation MySearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self myInitialize];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self myInitialize];
}
-(void)myInitialize
{
self.backgroundImage = [UIImage imageNamed:#"image.png"];
for (UIView* subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
//customize text field
UITextField* textfield = (UITextField*) subview;
}
}
}
#end