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

ios7自动布局按钮调整大小问题

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

我使用的是 xcode 5, Storyboard和自动布局已打开。我的一个 ViewController 的布局有一个带有图像背景的 UIButton。 View Controller 还有 3 个锚定在 View 底部的按钮和上面的一些标签。我将我的 Storyboard布置在 4 英寸视网膜显示器(即:iphone 5+)上,而我遇到的问题是在模拟 3.5 英寸显示器时(即:iphone 4、4s 等)

我已将锚定按钮的高度固定在底部以保持不变。我想要的是当应用程序在 3.5 英寸显示屏 iphone 上运行时,带有图像的 UIButton 将调整为更小(保持所有其他标签和按钮的大小和间距相同)。因此,该 UIButton 的纵横比将保持不变,它只会变小。

我无法在自动布局教程或其他在线教程中找到有关如何设置约束来执行此操作的任何内容(高度/宽度保持成比例)。



Best Answer-推荐答案


我认为您真正想要的如下: 1.Label顶部距离superView 100,底部距离superView 68 2.在 4"显示器中,其尺寸为 200x400,比例为 0.5 3.3.5寸显示器,尺寸为312x624,比例为0.5

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // create search bar
    CGRect frame = CGRectMake(60, 100, 200, 400);
    _label = [[UILabel alloc] initWithFrame:frame];
    _label.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_label];

    // layout search bar
    _label.translatesAutoresizingMaskIntoConstraints = NO;
    // height
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_label
                                                                  attribute:NSLayoutAttributeHeight
                                                                  relatedBy:NSLayoutRelationLessThanOrEqual
                                                                     toItem:nil
                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                 multiplier:0
                                                                   constant:400];
    // width
    [_label addConstraint:constraint];
    constraint = [NSLayoutConstraint constraintWithItem:_label
                                              attribute:NSLayoutAttributeWidth
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:_label
                                              attribute:NSLayoutAttributeHeight
                                             multiplier:0.5
                                               constant:0];
    [_label addConstraint:constraint];

    // vertical
    NSDictionary *views = NSDictionaryOfVariableBindings(_label, self.view);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat"V:|-100-[_label]-68-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];
    [self.view addConstraints:constraints];

    // horizontal
    constraint = [NSLayoutConstraint constraintWithItem:_label
                                              attribute:NSLayoutAttributeCenterX
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                              attribute:NSLayoutAttributeCenterX
                                             multiplier:1
                                               constant:0];
    [self.view addConstraint:constraint];
}

关于ios7自动布局按钮调整大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19804062/

回复

使用道具 举报

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

本版积分规则

关注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