• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何以更好的方式以编程方式布局 UI 元素?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:32:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在以编程方式布置用户界面,如下面的代码。有没有更好的方法以编程方式做到这一点?例如使用设计模式或我什至不知道它存在的东西。 我正在寻找更好的方法的原因是,当我必须更改 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 *) createButtonWithFrameCGRect) frame titleNSString *)title imageUIImage *) 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;
}



Best Answer-推荐答案


你应该使用 NSLayoutContraint .它为您的 UI 设计提供了更清洁和可移植的代码。如果您是新手,Ray Wenderlish 提供了一个不错的两部分 tutorial .

关于ios - 如何以更好的方式以编程方式布局 UI 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18759230/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap