【iOS】UIView


UIView 兩三事。

  • 使用 hex 設成特殊色背景,可先用 Digital Color 測量器,取色後將值填入 UIColorFromRGB  

    #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

    [profileListPanel setBackgroundColor:UIColorFromRGB(0xDDDFE4)];

  • 製作圓角 UIView

    [aView setAlpha:0.9f];
    [[aView layer] setBorderWidth:4.0f];
    [[aView layer] setBorderColor:[UIColor clearColor].CGColor];
    [[aView layer] setMasksToBounds:YES];
    [[aView layer] setBackgroundColor:[UIColor clearColor].CGColor];
    [[aView layer] setCornerRadius:10.0f];

  • 對齊方式共有
    - UIViewAutoresizingNone
    - UIViewAutoresizingFlexibleWidth
    - UIViewAutoresizingFlexibleHeight
    - UIViewAutoresizingFlexibleTopMargin
    - UIViewAutoresizingFlexibleBottomMargin
    - UIViewAutoresizingFlexibleLeftMargin
    - UIViewAutoresizingFlexibleRightMargin
  • 有將視窗顯示出類似 UIPopoverController/UIToolBar 的需求(Framed Table),但一些邊框顏色有些許不同時, 中間也需自己調整高度的話,可以切成三張圖,分別利用三個UIImageView 來貼出背景圖。
    - dialogbox_bg_top (類似UIPopoverController 上半部,大小為 484x78)
    - dialogbox_bg_center ( 類似UIPopoverController 中間部 484x1 ,高度為 1 讓它可自行延展)
    - dialogbox_bg_end (類似UIPopoverController 下半部 484x78)

    UIImageView *imageViewTop = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dialogbox_bg_top.png"]];
    imageViewTop.frame = CGRectMake(0.0f, 0.0f, 484.0f, 78.0f);
    [aView addSubview: imageViewTop];
    [aView bringSubviewToFront:imageViewTop];
    [imageViewTop release];
    imageViewTop = nil;

    UIImageView *imageViewCenter = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dialogbox_bg_center.png"]];
    if (profilePurchased)
    {
    imageViewCenter.frame = CGRectMake(0.0f, 78.0f, 484.0f, WITHOUT_SOME_LAYOUT - (78.0f*2));
    }else {
    imageViewCenter.frame = CGRectMake(0.0f, 78.0f, 484.0f, 648.0f-78.0f*2);
    }

    [aView addSubview: imageViewCenter];
    [aView bringSubviewToFront:imageViewCenter];
    [imageViewCenter release];
    imageViewCenter = nil;

    UIImageView *imageViewEnd = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dialogbox_bg_end.png"]];
    if (profilePurchased)
    {
    imageViewEnd.frame = CGRectMake(0.0f, WITHOUT_SOME_LAYOUT - 78.0f, 484.0f, 78.0f);
    }else {
    imageViewEnd.frame = CGRectMake(0.0f, 648.0f - 78.0f, 484.0f, 78.0f);
    }   
    [aView addSubview: imageViewEnd];
    [aView bringSubviewToFront:imageViewEnd];
    [imageViewEnd release];
    imageViewEnd = nil;

  • 承上,這樣的話原本是在UIToolBar/UIBarButtonItem的就要自行貼入所需要的UIButton。範例如下

    UIImage *btnCancelImg = [UIImage imageNamed:@"dialog_btn_bg_black.png"];
    UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnCancel setAlpha:0.9f];
    [btnCancel setFrame:CGRectMake(12.0, 6.0, 50.0, 29.0)];
    [btnCancel setBackgroundImage:btnCancelImg forState:UIControlStateNormal];
    [btnCancel setTitle:@"Cancel" forState:UIControlStateNormal];
    [btnCancel.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12]];
    [btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnCancel addTarget: self action: @selector(doCancel:) forControlEvents: UIControlEventTouchDown];
    [aView addSubview:btnCancel];

  • 原本的顯示文字也要自行貼入,範例如下

    UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(160.0f, 10.0f, 200.0f, 20.0f)];
    aLabel.text = @"Game Pad Rack";
    aLabel.textColor = [UIColor whiteColor];
    [aLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20.0f]];
    aLabel.backgroundColor = [UIColor clearColor];
    aLabel.font = [UIFont boldSystemFontOfSize:20.0f];
    [aLabel setBackgroundColor:[UIColor clearColor]];
    [aView addSubview:labGamePadRack];
    [aLabel release];

  • removeFromSuperview/
  • viewWithTag
  • 遞迴處理所有子視圖的方式
    for (UIView *subview in [aView subviews])
    {
    }
 

Ed32. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com