Saturday, October 5, 2013

UIButtonをプログラムから作成する

このようなフラットデザインのボタンを作れるライブラリを使いながらボタンを作成したのでめもめも
QBFlatDesignについてはこちらのgithubプロジェクトを見ていただければわかるので、ここでは触れません。

では、ボタンをプログラムから作る方法と、クリックイベントの取得の方法をばここに。
    QBFlatButton *bottomButton = [[QBFlatButton alloc]initWithFrame: CGRectMake(20, frame.size.height - 80, frame.size.width - 40, 60)];
    // デフォルトのボタンを作るときは以下のコード
     UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // この場合あとでフレームを指定しないと行けない
    // font
    bottomButton.titleLabel.font = [UIFont boldSystemFontOfSize: 20];
    [bottomButton setTitle:@"Mark" forState:UIControlStateNormal];
    [bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    // color
    bottomButton.faceColor = [UIColor colorWithRed:0.23 green:0.83 blue:.51 alpha:1];
    bottomButton.sideColor = [UIColor colorWithRed:.16 green:.76 blue:.44 alpha:1];
    // event
    [bottomButton addTarget:self action:@selector(bottomButtonTapped) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bottomButton];

No comments:

Post a Comment