V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ren2881971
V2EX  ›  问与答

请问在写 autolayout 约束时候怎么优雅的完成控件顶部距离为父视图高度的等比?

  •  
  •   ren2881971 · 2015-08-02 10:47:07 +08:00 · 2271 次点击
    这是一个创建于 3187 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如 我有一个imageView 加在自身的view上。
    想让imageView 距离顶部的距离为 view高度的1/4 应该怎么写?

    我自己写的:

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.25 constant:0]];
    

    我理解的 imageView.top = self.view.height*0.25 + 0 的逻辑感觉没什么问题,但是运行报错。。 求解答。
    'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes'

    然后我在stackoverflow 上找到答案 是向下面这么写:

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.25 constant:0]];
    

    运行成功。 但我不能理解
    imageView.top = self.view.bottom *0.25 + 0

    这里self.view.bottom是多少? view是self.veiw 父视图了~

    他的bottom多少呢? 又是相对什么而言得出的数值呢? 理解不能 求大神解答!

    2 条回复    2015-08-02 23:00:01 +08:00
    SeanChense
        1
    SeanChense  
       2015-08-02 19:28:18 +08:00
    你需要 Masonry
    ren2881971
        2
    ren2881971  
    OP
       2015-08-02 23:00:01 +08:00
    @SeanChense 额。。 非得使用第三方框架么?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5386 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:25 · PVG 15:25 · LAX 00:25 · JFK 03:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.