2

以下のコードのように、ユーザー インターフェイスをプログラムでレイアウトしています。プログラムでこれを行うより良い方法はありますか? たとえば、デザイン パターンや、それが存在することさえ認識していないものを使用します。私がより良い方法を探している理由は、UI のデザインやレイアウトを変更しなければならないとき、それが本当に見苦しく乱雑に感じられるからです。

- (void) loadView
{
    [super loadVIew];

    self.view.backgroundColor = [UIColor whiteColor];

    self.title = @"ペットショップ";

    float y = 44;
    float x = 0;
    float width = self.view.frame.size.width;
    float height = width * .6;

    if (!topPictureView_) {
        topPictureView_ = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        topPictureView_.backgroundColor = [UIColor lightGrayColor];
        topPictureView_.image = [UIImage imageNamed:@"info_bg"];
        topPictureView_.contentMode = UIViewContentModeScaleAspectFill;
        [self.view addSubview:topPictureView_];
        [self.view sendSubviewToBack:topPictureView_];
    }

    y+=height-35;
    height = 35;

    if (!lblBranchName_) {
        lblBranchName_ = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height  )];
        lblBranchName_.textColor = [UIColor colorWithWhite:.3 alpha:1];
        lblBranchName_.backgroundColor = [UIColor colorWithWhite:0.8 alpha:.7];
        lblBranchName_.textAlignment = NSTextAlignmentCenter;
        lblBranchName_.text = @"ペットショップ KOMATSU";
        lblBranchName_.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
        [self.view addSubview:lblBranchName_];
    }

    y+=height;
    height = 45*4;

    y+=5;

    height = 50;

    float col = self.view.frame.size.width/2;
    float margin = 5;

    btn11_ = [self createButtonWithFrame:CGRectMake(margin, y, col-margin*1.5, height) title:@"11" image:[UIImage imageNamed:@"menu"]];
    btn12_ = [self createButtonWithFrame:CGRectMake(col+margin/2, y, col-margin*1.5, height) title:@"12" image:[UIImage imageNamed:@"order"]];
    y+= height+margin;

    btn21_ = [self createButtonWithFrame:CGRectMake(margin, y, col-margin*1.5, height) title:@"21" image:[UIImage imageNamed:@"order"]];
    btn22_ = [self createButtonWithFrame:CGRectMake(col+margin/2, y, col-margin*1.5, height) title:@"22" image:[UIImage imageNamed:@"map"]];

    y+= height+margin;

    btnChat = [self createButtonWithFrame:CGRectMake(margin, y, col*2-margin*2, height) title:@"Chat" image:nil];


    y+=height+margin;
    height = self.view.frame.size.height-y;

    if (!eventBanner_) {
        eventBanner_ = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height )];
        eventBanner_.image = [UIImage imageNamed:@"banner"];
        [self.view addSubview:eventBanner_];
    }
}
- (UIButton *) createButtonWithFrame:(CGRect) frame title:(NSString *)title image:(UIImage *) image {
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateSelected];
    [[btn layer]setBorderWidth:1.0f  ];
    [[btn layer] setBorderColor:[UIColor grayColor].CGColor];
    [[btn layer] setCornerRadius:10.0f];
    btn.clipsToBounds = YES;
    [self.view addSubview:btn];
    return btn;
}
4

1 に答える 1

1

NSLayoutContraintを使用する必要があります。これにより、UI 設計用のよりクリーンで移植可能なコードが作成されます。初めての方は、Ray Wenderlish が 2 部構成のチュートリアルを提供しています。

于 2013-09-12T08:41:33.397 に答える