首页 \ 问答 \ 如何为水平图像视图的自动布局正确设置约束?(How to properly set constrains for Auto layout for horizontal image views?)

如何为水平图像视图的自动布局正确设置约束?(How to properly set constrains for Auto layout for horizontal image views?)

我遇到了自动布局的问题。 更具体地说,当您有一个相当复杂(或可能是紧凑)的布局时,我对自动布局的工作原理感到困惑。 当我尝试进行水平布局时,我甚至都没有成功过。 例如,我尝试在故事板中水平设置两个标签,无论我做什么,在实际设备上检查它时都是热点。

所以这一次,我的噩梦又来了。 这里的截图是我在故事板中设置的视图。 我正在使用自动布局,所以我必须添加约束,我首先要做的是为第一个图像视图添加顶部空间约束(左上角),它告诉我视图不明确。 这似乎不对,因为在这样的情况下,Xcode总是告诉我图像视图需要斧头位置。 但这一次它“含糊不清”。 我尝试了很多东西,但无论如何都没有锻炼。 我放弃了手动设置,而是让Xcode为我做,我点击“重置为建议的约束”,然后设置相等的宽度和高度,然后将图像视图设置为1:1方面无线电。 但是在预览时,它仍然是一个热点。

显然,我和Xcode都不能像我们预期的那样设置布局。 这就是为什么我来这里问你们,让我为你解释一下我的担忧:1。我应该为所有图像视图设置什么约束? 2.我应该为“主题”设置什么约束? 3.我应该为标签设置什么约束(主题下面的描述)?

提前致谢。

截图


I am experiencing an issue with auto layout. More specifically, I am confused by how Auto Layout works when you have a rather complex(or maybe compact) layout. I never even succeed once when I tried to do a horizontal layout. For example, I tried to set two labels horizontally in storyboard before, no matter what I do, it's hot mess when checking it on an actual device.

So this time, my nightmare comes again. The screenshot here is the view I set up in the storyboard. I am using auto layout, so I must add constrains, what I did first is add a top space constrain for the first image view(top left), and it told me views are ambiguous. That doesn't seem right, because in circumstances like these, Xcode always tell me the image view needs a x position. But this time it's "ambiguous". And I tried many things, it didn't workout anyway. I gave up setting up manually, instead I ask the Xcode to do it for me, I tapped "reset to suggested constrains", and then set equal width and height, and then set the image views to 1:1 aspect radio. But when previewing, it's still a hot mess.

So apparently, neither me nor Xcode could set up the layout behaved just as expected. That's why I come here and ask you guys, let's me break it down for you about my concerns: 1. What constrains should I set for all the image views? 2. What constrains should I set for "topic"? 3. What constrains should I set for the label (description below the topic)?

Thanks in advance.

Screenshot


原文:https://stackoverflow.com/questions/38930616
更新时间:2022-08-23 22:08

最满意答案

纯CSS方法:

您可以使用按钮的属性来检测导航栏是否在较小的屏幕中折叠或展开。

@media (max-width: 768px) {
  button[aria-expanded="true"] ~ .navbar-brand.expanded-logo {
    display: block; /* If aria-expanded is set to true, show the logo for expanded state */
  }
  button[aria-expanded="false"] ~ .navbar-brand.collapsed-logo {
    display: block; /* If aria-expanded is set to false, show the logo for collapsed state */
  }
  button[aria-expanded="true"] ~ .navbar-brand.collapsed-logo {
    display: none; /* Hide the collapsed state logo when the expanded state logo is shown */
  }
}
.expanded-logo {
  display: none; /* Show only one logo on larger screen, hide logo by default */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div data-example-id="navbar-static-top" class="bs-example bs-navbar-top-example">
  <nav class="navbar navbar-default navbar-static-top">
    <!-- We use the fluid option here to avoid overriding the fixed width of a normal container within the narrow content columns. -->
    <div class="container-fluid">
      <div class="navbar-header">
        <button aria-expanded="false" data-target="#bs-example-navbar-collapse-8" data-toggle="collapse" class="navbar-toggle collapsed" type="button">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a href="#" class="navbar-brand collapsed-logo">
          <img src="http://placehold.it/30x30/333">
        </a>
        <a href="#" class="navbar-brand expanded-logo">
          <img src="http://placehold.it/30x30/666">
        </a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div id="bs-example-navbar-collapse-8" class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a>
          </li>
          <li><a href="#">Link</a>
          </li>
          <li><a href="#">Link</a>
          </li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
  </nav>
</div>


Pure CSS approach:

You can use the attributes of the button to detect if the navbar is collapsed or expanded in smaller screens.

@media (max-width: 768px) {
  button[aria-expanded="true"] ~ .navbar-brand.expanded-logo {
    display: block; /* If aria-expanded is set to true, show the logo for expanded state */
  }
  button[aria-expanded="false"] ~ .navbar-brand.collapsed-logo {
    display: block; /* If aria-expanded is set to false, show the logo for collapsed state */
  }
  button[aria-expanded="true"] ~ .navbar-brand.collapsed-logo {
    display: none; /* Hide the collapsed state logo when the expanded state logo is shown */
  }
}
.expanded-logo {
  display: none; /* Show only one logo on larger screen, hide logo by default */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div data-example-id="navbar-static-top" class="bs-example bs-navbar-top-example">
  <nav class="navbar navbar-default navbar-static-top">
    <!-- We use the fluid option here to avoid overriding the fixed width of a normal container within the narrow content columns. -->
    <div class="container-fluid">
      <div class="navbar-header">
        <button aria-expanded="false" data-target="#bs-example-navbar-collapse-8" data-toggle="collapse" class="navbar-toggle collapsed" type="button">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a href="#" class="navbar-brand collapsed-logo">
          <img src="http://placehold.it/30x30/333">
        </a>
        <a href="#" class="navbar-brand expanded-logo">
          <img src="http://placehold.it/30x30/666">
        </a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div id="bs-example-navbar-collapse-8" class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a>
          </li>
          <li><a href="#">Link</a>
          </li>
          <li><a href="#">Link</a>
          </li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
  </nav>
</div>

相关问答

更多
  • 你用 #main_text .title { /*attributes*/ } 如果您在选择器之间放置一个空格,则样式将应用于第一个的所有儿童(以及儿童的孩子)。 因此,在这种情况下,#main_text的任何子元素的类名称为.title。 如果使用>而不是空格,则只会选择元素的直接子元素,而不是子元素的子元素,例如: #main_text>.title { /*attributes*/ } 要么在这种情况下工作,但第一种更常用。 you use #main_text .title { ...
  • 是否有一个实例,你特别想在动画中只选择div ? 您对脚本的这种修改是否会产生所需的结果? $('.gbox').hover( function() { $(this).children().animate({height: '+=10px'}, 200) }, function() { $(this).children().animate({height: '-=10px'}, 200) } ); Is there an instance where you'd specifically ...
  • 您可以使用jquery CSS方法从元素中获取背景颜色: http : //api.jquery.com/css/ var color = $(this).css("background-color"); You can use the jquery CSS method to get the background color from an element: http://api.jquery.com/css/ var color = $(this).css("background-color");
  • 纯CSS方法: 您可以使用按钮的属性来检测导航栏是否在较小的屏幕中折叠或展开。 @media (max-width: 768px) { button[aria-expanded="true"] ~ .navbar-brand.expanded-logo { display: block; /* If aria-expanded is set to true, show the logo for expanded state */ } button[aria-expanded="f ...
  • 回答 简要 根据您提供的示例,以下正则表达式将起作用,但是,它不适用于所有CSS规则。 如果添加更多案例,我可以更新正则表达式以适应其他情况。 码 请参阅此处使用的正则表达式 正则表达式 (?:^|,\s*)\K(\.foo-bar)(?=\s*(?:,|{)) 替代 UNUSED $1 注意:使用多行m标志。 用法 regex101生成以下脚本(通过单击regex101中的代码生成器): 链接在此处 $re = '/(?:^|,\s*)\K(\.foo-bar)(?=\s*(?:,|{))/m'; $ ...
  • body div .topNav { background-image: url("images/topNav-bg.png"); background-repeat: repeat-x; } 我认为这可能有用 body div .topNav { background-image: url("images/topNav-bg.png"); background-repeat: repeat-x; } I think this might work
  • Enyo 2.0改变了为组件指定类的方式。 您不应再在类型定义中指定样式或类。 如果您需要这样做,您可以使用“类”而不是“className”。 推荐的方法是在create function中调用addClasses(在Enyo 1.0中是addClass)。 要直接添加样式,请调用addStyles。 Enyo 2.0 changed the way you specify classes for your components. You should no longer specify styles o ...
  • 认为您需要将样式表声明更改为: button.btnBlue {样式去这里} 按钮:: - moz-focus-inner.btnBlue {样式去这里} 按钮:hover.btnBlue,按钮:focus.btnBlue {styles go here} 按钮:active.btnBlue {样式转到此处} Think you need to change the stylesheet declarations here to: button.btnBlue{ Styles go here } butto ...
  • 首先,好消息是: 我的问题是,当你第一次访问该网站时,它会弹出一个顶部的横幅,用户必须单击,然后单击“允许”,然后单击“确定”。 我不认为大多数IE用户会这样做。 大多数IE用户不必这样做,因为在“运行ActiveX控件和插件”的默认安全设置中, “已启用”。 我怀疑您是否看到它,因为您或您的网络管理员故意在您的安装上锁定ActiveX。 现在是坏消息 - 我不确定是否有任何方法可以测试ActiveX而不会触发默认情况下未启用它的人的警告消息。 但是,如果你可以为它进行可靠的测试(不知道具体如何,但应用过滤 ...
  • 嘿,你可以这样做..假设你有一个看起来像这样的iframe和b的iframe。 码 现在假设你有一个包含iframe的主页面。你可以编写像这样的代码。检查。 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)