iOS7 UI改变总结

来源:互联网 发布:python 开源项目 编辑:程序博客网 时间:2024/04/19 06:35

苹果官方文档:

https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TransitionGuide/ContentViews.html#//apple_ref/doc/uid/TP40013174-CH10-SW1


IOS7在UI方面发生了很大改变,所以感觉有必要重新审视的学习一下(新特性+以前未注意到的特性)。

现在开始了:


1、UIView:


a)view.clearsContextBeforeDrawing =YES  <IOS6>

When the Clears Graphics Context (clearsContextBeforeDrawing) checkbox is selected, the drawingbuffer is automatically cleared to transparent black before the view is drawn. This behavior ensures thatthere are no visual artifacts left over when the view’s contents are redrawn. (UIView每次重会之前,清除掉上次的内容,由于系统默认就是YES,所以忽略这个特性了,这个特性不是IOS7才有的)


b)Appearance Proxies  <IOS6>

代码和效果图:

1>To customize the appearance of all instances of a class, useappearanceto get the appearance proxy forthe class (让某一类控件同时表现某种属性)

    [[UIButton appearance]setBackgroundColor:[UIColorredColor]];

    [[UIButton appearance]setTitle:@"ssss"forState:UIControlStateNormal];

2>

To customize the appearances for instances of a class when contained within an instance of a containerclass, or instances in a hierarchy, you useappearanceWhenContainedIn:to get the appearance proxyfor the class. (

   
  
 

让某一类控件在另一种控件中同时表现某种属性

   
  
 

)

    

[[UIButton  appearanceWhenContainedIn:[UIView  class],nil]  setTitleColor:[UIColor   greenColor]

                                                                   forState:UIControlStateNormal];



Appearance Proxies 特性在IOS7以前可用,但并不那么显眼,但IOS7之后,由于UI的外表的改变,看起来更明显了,如图:


注意:

You can use an appearance proxy to set particular appearance properties for all instances of a view in yourapplication. (所有继承自UIView的控件都有这个特征,只不过不同的控件会以不同的方式展现这个特性)

更多代码:

[[UISlider appearance] setMinimumTrackTintColor:[UIColor greenColor]];
[[UISlider appearanceWhenContainedIn:[UIView class], nil]
            setMinimumTrackTintColor:[UIColor greenColor]];


c)Using Template Images <IOS7>

模版图片:

UIImage *myImage = [UIImage imageNamed:@"myImageFile.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];


UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used
UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template
UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information
这个不理解有什么用,高人指点~~

d)

Using Auto Layout with Views 

看这几篇,应该可以入门:

http://blog.csdn.net/zfpp25_/article/details/8861221


2、
   
  
 

UIActionSheet

   
  
 
:<IOS7样式>



不要吃惊,屏幕下方的真的是UIActionSheet,实现方式没有改变,但就是这样式……

UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:nil

                                                             delegate:nil

                                                    cancelButtonTitle:@"Cancel"

                                               destructiveButtonTitle:@"Delete"

                                                    otherButtonTitles:@"one"@"two",nil];

  [actionSheet showInView:self.view];


3、UIActivityIndicatorView
   
  
 

    [[UIActivityIndicatorViewappearance]setColor:[UIColorblueColor]];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator.frame = CGRectMake(180,100,100,100);

    [self.view addSubview:indicator];

    [indicator startAnimating];


   这样一看,转圈也好像顺眼多了。


4、UIAlertView:
   
  
 
<IOS7样式>
   
  
 

实现方式没有改变



注意:

1、做版本兼容时候,发现 UIAlertView 的 visible 属性貌似失效了。


5、UICollectionView  
   
  
 
<IOS6>
   
  
 

高级控件,适合做相册效果:http://blog.sina.com.cn/s/blog_5a6efa330101doc9.html



1、UINavigationBar:




    NSDictionary* attrs = @{NSForegroundColorAttributeName: [UIColorblackColor],

                            NSFontAttributeName: [UIFontfontWithName:@"AmericanTypewriter"size:0.0],

                            };

    [[UINavigationBar appearancesetTitleTextAttributes:attrs];

    [[UINavigationBar appearancesetTintColor:[UIColorredColor]];

    

    UINavigationBar *navBar = [[UINavigationBaralloc]init];

    navBar.frame = CGRectMake(0,50,32044);

    

    [navBar pushNavigationItem:[[UINavigationItemalloc]initWithTitle:@"Test"]animated:YES];

    [navBar pushNavigationItem:[[UINavigationItemalloc]initWithTitle:@"Test"]animated:YES];

    [navBar pushNavigationItem:[[UINavigationItemalloc]initWithTitle:@"Test"]animated:YES];

    [navBar pushNavigationItem:[[UINavigationItemalloc]initWithTitle:@"Test"]animated:YES];


    [navBar setBarStyle:UIBarStyleDefault];

    [navBar setBackgroundImage:[UIImageimageNamed:@"bg.png"]forBarMetrics:UIBarMetricsDefault];

    [self.view addSubview:navBar];

    


2、UIProgressView





{

    progress = [[UIProgressViewalloc]initWithFrame:CGRectMake(10,100,300,10)];

    [progresssetProgressImage:[UIImageimageNamed:@"bg.png"]];

    [progresssetProgressViewStyle:UIProgressViewStyleBar];

    [progresssetTrackImage:[UIImageimageNamed:@"sss.png"]];

    [self.viewaddSubview:progress];

    

    [NSTimer scheduledTimerWithTimeInterval:1.0target:selfselector:@selector(run)userInfo:nilrepeats:YES]; 

    

}


-(void) run

{

    progress.progress +=0.1;

    [progresssetProgress:progress.progressanimated:YES];

}




2、UISearchbar




    UISearchBar *searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,20,320,44)];

    [searchBar setBarStyle:UIBarStyleDefault];

    [searchBar setBarTintColor:[UIColorredColor]];

    [searchBar setBackgroundImage:[UIImageimageNamed:@"bg.png"]

                   forBarPosition:UIBarPositionAny

                       barMetrics:UIBarMetricsDefault];

    

    searchBar.showsCancelButton =YES;

    [self.view addSubview:searchBar];



1、UITextView:

A )      IOS7新增加的 UITextViewDelegate 方法:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRangeNS_AVAILABLE_IOS(7_0);


这个代理方法是当用户点击UITextView中的超链接的时候回调次方法:

看代码:

1、首先viewController.h 文件中声明:<UITextViewDelegate>

2、viewController.m 中添加如下代码:

    

UITextView *textView;

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    UIMenuItem *menuItem = [[UIMenuItemalloc]initWithTitle:@"替换"action:@selector(changeColor:)];

    UIMenuController *menu = [UIMenuControllersharedMenuController];

    [menu setMenuItems:[NSArrayarrayWithObject:menuItem]];

    [menuItem release];


    textView = [[UITextViewalloc]initWithFrame:[UIScreenmainScreen].applicationFrame];

    textView.delegate =self;

    textView.dataDetectorTypes =UIDataDetectorTypeAll;

    

    textView.editable =NO;//(必须的)

    

    textView.attributedText = [[NSAttributedStringalloc]initWithString:

                               @"My phone number is +8602980000000.\r\n"

                                "My personal web site www.xxxxxx.com.\r\n"

                                "My E-mail address is XXXXX@gmail.com.\r\n"

                         "I was born in 1900-01-01."];

    

    [self.viewaddSubview:textView];


}


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange

{

    if (URL)

    {

        NSLog(@"%@", [URLabsoluteString]);

        

        return NO;

    }

    

    return YES;

}


- (void) changeColor:(id) sender

{

    NSLog(@"changeColor");

}


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

    if(action ==@selector(changeText:))

    {

        if(textView.selectedRange.length>0)

            return YES;

    }

    return NO;

}

运行项目,点击其中的超链接就会回调 - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange 方法,然后在这个方法里实现项目需求。


注意:NSAttributedString 用法见:


http://blog.csdn.net/zfpp25_/article/details/9143299

http://blog.csdn.net/zfpp25_/article/details/8639215



点击网址后,控制台打印



附加:

UIMenuItem 效果:


B )    

1、  UITextView IOS7 中新增加属性  BOOL selectable

@property(nonatomic,getter=isSelectable)BOOL selectableNS_AVAILABLE_IOS(7_0);// toggle selectability, which controls the ability of the user to select content and interact with URLs & attachments

用法:决定UITextView 中文本是否可以相应用户的触摸,主要指:1、文本中URL是否可以被点击;2、UIMenuItem是否可以响应


2、UITextView IOS7 中新增加属性  NSTextContainer *textContainer,意思是UITextView的文本输入容器,感觉是把以前的私有API公开了出来!有了这个 属性,就可以设置文本的对齐方式等等

// Set and get the text container for the text view

@property(nonatomic,readonly)NSTextContainer *textContainerNS_AVAILABLE_IOS(7_0);


例如:

textView.textContainer.lineBreakMode =NSLineBreakByTruncatingTail;

lineBreakMode 的其他参数如下:

typedef enum {
   UILineBreakModeWordWrap = 0,        以单词为单位换行,以单位为单位截断。
   UILineBreakModeCharacterWrap,       以字符为单位换行,以字符为单位截断。
   UILineBreakModeClip,                        以单词为单位换行。以字符为单位截断。
   UILineBreakModeHeadTruncation,     以单词为单位换行。如果是单行,则开始部分有省略号。如果是多行,则中间有省略号,省略号后面有4个字符。
   UILineBreakModeTailTruncation,         以单词为单位换行。无论是单行还是多行,都是末尾有省略号。
   UILineBreakModeMiddleTruncation,    以单词为单位换行。无论是单行还是多行,都是中间有省略号,省略号后面只有2个字符 
} UILineBreakMode;


3、  UITextView IOS7 中新增加属性  BOOL editable 

By default, users can add, remove, or change text within a text view. (默认是YES)可以添加、移出、更改UITextView的text。



1、UIToolBar:


IOS7中增加UIToolbarDelegate,当UIToolbar呈现时候回调此方法:


看代码:

- (void)viewDidLoad

{

    [superviewDidLoad];

    

    UIToolbar *toolbar = [[UIToolbaralloc]initWithFrame:CGRectMake(0,20,320,44)];

    toolbar.barStyle = UIBarStyleBlack;

    toolbar.delegate = self;

    

    UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithTitle:@"Text"style:UIBarButtonItemStyleBorderedtarget:nilaction:nil];

    [toolbar setItems:[NSArrayarrayWithObjects:item,nil]];

    

    [self.view addSubview:toolbar];

}


/* Implement this method on your manual bar delegate when not managed by a UIKit controller.

 UINavigationBar and UISearchBar default to UIBarPositionTop, UIToolbar defaults to UIBarPositionBottom.

 This message will be sent when the bar moves to a window.

 */


- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar

{

    NSLog(@"positionForBar");

    returnUIBarPositionTop;

}


设置ToolBar上边沿的阴影:

/* Default is nil. When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage:forToolbarPosition:barMetrics: (if the default background image is used, the default shadow image will be used).
 */
- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
- (UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;




1、UITableView:



UITableViewDelegate 新增内容:

// Use the estimatedHeigh(估算高度)t methods to quickly calcuate guessed values which will allow for fast load times of the table.

// If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

{

   return50;

}  //  这个方法先返回一个估算的cell高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{    kPrintInfo

    return 40;

} 然后这个方法才返回真正的cell高度


这两个方法同理

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;


新增属性:

@property(nonatomic)         CGFloat                    estimatedRowHeight ;// default is 0, which means there is no estimate

@property(nonatomic)         CGFloat                    estimatedSectionHeaderHeight ;// default is 0, which means there is no estimate

@property(nonatomic)         CGFloat                    estimatedSectionFooterHeight ;// default is 0, 

通过新增代理放大不难知道,上述三个新增属性不难理解了。


// the background color of the section index while not being touched(当section不被触摸时候的背景颜色)

@property(nonatomic,retain)UIColor *sectionIndexBackgroundColor;         


2、UIButton:

UIButton的这个属性是 IOS6引入的,以前没注意到:

- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)stateNS_AVAILABLE_IOS(6_0);// default is nil. title is assumed to be single line

用法如下:

- (NSMutableAttributedString *) getString

{

    NSMutableAttributedString *attriString = [[NSMutableAttributedStringalloc]initWithString:@"this is test!"];


    //改变this的字体,value必须是一个CTFontRef

    [attriString addAttribute:(NSString *)kCTFontAttributeName

                        value:CFBridgingRelease(CTFontCreateWithName((CFStringRef)[UIFontboldSystemFontOfSize:14].fontName,14,NULL))

                        range:NSMakeRange(0,4)];

    //this加上下划线,value可以在指定的枚举中选择 

    [attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName 

                        value:(id)[NSNumbernumberWithInt:kCTUnderlineStyleDouble

                        range:NSMakeRange(0,4)]; 

    return attriString;

}


- (void)viewDidLoad

{

    [superviewDidLoad];


    [btsetAttributedTitle:[selfgetString]forState:UIControlStateNormal];

}


3、UIDatePicker:


不想说什么了~


4、UISteper:

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    UIStepper *myStepper = [[UIStepper allocinitWithFrame:CGRectMake(01032050)];

    myStepper.backgroundColor = [UIColor redColor];

    [myStepper addTarget:self

                       action:@selector(myAction:)

             forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:myStepper];

}


- (void) myAction:(UIStepper *) sender

    CFShow((__bridge CFTypeRef)(@(sender.value)));

}