【iOS】UIButton


幾個重點 (tbd)
  • Button類型
    typedef enum {
       UIButtonTypeCustom = 0,
       UIButtonTypeRoundedRect,
       UIButtonTypeDetailDisclosure,
       UIButtonTypeInfoLight,
       UIButtonTypeInfoDark,
       UIButtonTypeContactAdd,
    } UIButtonType;
  • 注意採用 buttonWithType 的情況,
    - 宣告時有 alloc 的話,需要手動relase,例如
    button = [[UIButton alloc] init];
    - 若採用buttonWithType沒有使用alloc 的話,就不需要手動 release,否則在release時會crash。
    UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
  • Selected 需要自行更改
    - 設定圖片
    [button setBackgroundImage:someImage forState:UIControlStateSelected];
    - 自行更改 status,就可以江圖片顯示出來。
    self.checkInButton.selected = YES;
  • 基本用法

    UIButton *mouse = [UIButton buttonWithType:UIButtonTypeCustom];
    [mouse setFrame:CGRectMake(90.0f, 55.0f, 118.0f, 118.0f)];
    [mouse setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [mouse setTitle:@"Mouse" forState:UIControlStateNormal];
    [mouse.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
    [mouse setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [mouse addTarget: self action: @selector(mouseClick:) forControlEvents: UIControlEventTouchDown];
    [parent addSubview:mouse];

  • 漸層UIButton需要以UIButton 擴展的第三方元件,或由美工直接做出底圖。
    參考 Custom UIButton without using any images
  • CustomBadge
    參考 CustomBadge 或 Three320/TTButton 也有範例。

    問題
    可以用 UISegmentedControl 做出漸層的按鈕,但不知如何 binding 事件
    http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem
 

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