详解iPhone开发应用为视图加边框


  本文标签:iPhone开发 视图

  iPhone开发应用为视图加边框是本文要介绍的内容,主要来介绍视图中的一个有趣的案例,实现给视图加边框的效果,不多说,直接来看详细内容讲解  。通过层(layer),可以给视图增加边框和圆角等  。比如类似下面的效果,如图:

  详解iPhone开发应用为视图加边框

  写法:

  1. - (void)loadView {   
  2.     [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];   
  3.     UIImageView *contentView = [[MyUIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];    
  4.     [contentView setImage:[UIImage imageNamed:@"1.jpg"]];    
  5.     [contentView setUserInteractionEnabled:YES];   
  6.           
  7.     self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];   
  8.     [self.view addSubview:contentView];   
  9.     self.view.backgroundColor=[UIColor blackColor];   
  10.       
  11.     //为视图增加边框   
  12.     contentView.layer.masksToBounds=YES;   
  13.     contentView.layer.cornerRadius=20.0;   
  14.     contentView.layer.borderWidth=10.0;   
  15.     contentView.layer.borderColor=[[UIColor blueColor] CGColor];   
  16.     [contentView release];   

  主要看文字注释下面的四行代码  。也可以用上面注释掉的代码写法,但没有后面通过属性设置简明  。

  要完成这些代码,需要引入QuartzCore库  。在头文件中需要加入:

  1. #import <QuartzCore/QuartzCore.h> 

  在xcode项目的Frameworks部分加入,如图:

  详解iPhone开发应用为视图加边框

  小结:iPhone开发应用为视图加边框的内容介绍完了,希望通过本文的学习能对你有所帮助!