• iOS 5设备下UINavigationbar的背景修改

    最近我突然发现UINavigationbar背景修改的方法不起作用了,代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @implementation UINavigationBar (CustomImage)

    -(void)drawRect:(CGRect)rect
    {
        UIImage *image = [UIImage imageNamed:@"navigationbar.png"];
        [image drawInRect:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
    }

    @end

    发现原来是iOS 5的原因,如果运行在iOS 5以下的版本就没有问题了。经过实验以下方法适合iOS 5(放在ViewDidLoad中):

    1
    2
    3
        if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
            [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];
        }

    第一条if语句的作用是防止程序在iOS 5以下的版本中崩溃。

    这样,依靠这两段代码,我的UINavigationbar的背景问题在iOS 5及以下版本中得到了完美的解决。

     
  • 创建iOS 5 News Stand应用程序之一 – 外观

    iOS 5提供了一个新的framework – Newsstand framework,它允许把应用程序运行于News Stand中。实际上,News Stand相当于一个特制的文件夹专门放置报纸,杂志类应用程序。由于时间有限,这里我分几个部分介绍这一技术。


    首先介绍的就是怎样把一个应用程序改变成一个News Stand程序,这实际上有两步工作,一是让程序运行于News Stand,二是改变程序的图标。
    读文章 »

     
  • 强迫UIView以某种方向显示的秘诀

    我有一个项目其中某些UIView必须以特定的方向(Portrait或者Landscape)显示。这个看似简单的问题,困惑了我很久,直到今天我才完全找到解决的方法。
    读文章 »

     
  • presentModalViewController显示半透明UIView

    很多时候我们需要使用presentModalViewController来显示Modal View。如果需要显示半透明的Modal View应该怎么办呢?当然可以自己创建一个半透明的UIView,然后模拟presentModalViewController的动画效果。

    不过iOS 4以后的版本再也不需要怎么麻烦了,有一个非常简单的方法,示例如下(这段代码运行于一个View Controller中):

    1
    2
    3
    4
    5
    6
        UIViewController* transparentView = [[UIViewController alloc] init];           

           UIViewController* controller = self.view.window.rootViewController;
           transparentView.view.backgroundColor = [UIColor clearColor];
           controller.modalPresentationStyle = UIModalPresentationCurrentContext;        
           [controller presentModalViewController:transparentView animated:YES];

    其要点就是使用iOS特有的rootViewController来显示Modal View。

     
  • 分享一小段代码

    我们经常会遇到这种情况,某个按钮太小很难按到,又不想把按钮图片做得太大。我把它作成了一个Class Method,放在了我的Utils类中。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    +(void)resizeTouchableAreaForButton:(UIButton* )button withNewSize:(CGSize)size
    {
        // increase margin around button based on width
        const CGFloat margin_h = 0.5f * ( size.width - button.frame.size.width );
        const CGFloat margin_v = 0.5f * ( size.height - button.frame.size.height );
       
        // add margin on all four sides of the button
        CGRect newFrame = button.frame;
        newFrame.origin.x -= margin_h;
        newFrame.origin.y -= margin_v;
        newFrame.size.width  += 2.0f * margin_h;
        newFrame.size.height += 2.0f * margin_v;
       
        button.frame = newFrame;
    }

    这段代码把按钮的可按区域扩大到新的尺寸。不过有一个前提是使用UIButton的setImage而不是setBackgroundImage来设置按钮图片。

     
  • 检查UIWebView上touch的最简单的方法

    我有一个程序需要检测UIWebView是否有touch动作,不幸得很,UIWebView上的touchesBegan等事件无法被检测。在网上查了一下,有许多解决方法,比如在UIWebView上再加一个透明的UIView,重置UIWindow的sendEvent或重置UIWebView的hitest方法等等,要么就是方案不太完美,要么就是太过复杂,经过实验我使用的这种方法最为简单(否则我也没有时间写在这里了),当然我只要求检测UIWebView上有touch动作即可。我的方法是使用UITapGestureRecognizer。关键的地方有两点,请看我的代码(我的代码是在一个UIViewController中):

    1
    2
    3
    4
    5
        UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [self.view addGestureRecognizer:singleTap];
        singleTap.delegate = self;
        singleTap.cancelsTouchesInView = NO;
        [singleTap release];


    第三行是第一个关键的地方,就是必须设置UITapGuestureRecognizer的delegate(一般我们直接使用initWithTarget,无需设置其delegate)。

    第二个关键的地方是在下列delegate方法return YES。

    1
    2
    3
    4
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }


    做到这两点,你的UIWebView就可以响应touch事件了,你可以使用不同的UIGestureRecognizer来满足你不同的要求等。(handleSingleTap方法请自行补上)。

     
  • 怎样使UISearchBar背景透明

    在使用UISearchBar时,将背景色设定为clearColor,或者将translucent设为YES,都不能使背景透明,经过一番研究,发现了一种超级简单和实用的方法:

    1
    [[searchbar.subviews objectAtIndex:0]removeFromSuperview];

    背景完全消除了,只剩下搜索框本身了。

     
  • 访问外部网页

    实际上有两种方法,第一种是:

    1
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];



    这种方法将会退出当前程序,用Safari打开url。我有好几个朋友都问到这个问题,实际上非常简单,可以使用UIWebView在当前程序中打开网页(假定你的UIWebView实例为webView):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
         NSString *urlAddress = @"http://localhost";
           
        //Create a URL object.
        NSURL *url = [NSURL URLWithString:urlAddress];
           
        //URL Requst Object
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
           
        //Load the request in the UIWebView.
        [webView loadRequest:requestObj];
     
  • 以密码方式显示UITextField文本

    很简单,如下:

    1
    textField.secureTextEntry = YES;


     
  • 不规则形状的UIButton

    有的时候,我们需要使用非规则形状的按钮。UIButton允许你选择带有alpha通道的图像。比如,我使用下面四个图像:


    image


    然后用Interface Builder创建用户定义按钮,你可以透过图像的透明部分看到后面的按钮(假定按钮未定义为opaque)。.然而 UIButton 的点击测试(hit-testing)并未考虑图像的透明性,所以当你将图像重叠放置时,如图所示:


    image


    如果你点击此处:


    image



    默认的点击测试的结果是绿色菱形按钮被按下,而不是蓝色按钮。当然这可能就是你需要的效果,但大部分情况下并非如你所愿。那么怎样才能让你的程序正常工作?实际上很简单,你只需要一个UIButton的子类并重写点击测试方法。

    读文章 »