【问题标题】:Put different backgrounds in a View Controller在视图控制器中放置不同的背景
【发布时间】:2013-02-14 10:40:17
【问题描述】:

我正在做我在 iOS 中创建应用程序的第一步,所以也许我在问一个愚蠢的问题。 在视图控制器中,我放置了一些按钮和文本字段来执行不同的操作。为了改善组织,我想放置几个带边框的背景来分隔放置按钮和文本字段的区域。 我尝试放置一个没有文本和颜色背景的文本标签,并将我的对象移到它上面,但我不知道为什么将图层放置在按钮和文本字段上。然后,我尝试修改文本标签的不透明度,但完全不能令人满意。

label1.layer.cornerRadius = 5.0;
label1.layer.masksToBounds = YES;
label1.layer.borderColor = [UIColor lightGrayColor].CGColor;
label1.layer.borderWidth = 1.0;
label1.layer.opacity = 0.1;
label1.layer.backgroundColor = [UIColor whiteColor].CGColor;

任何人都可以帮助我找到更好的方法来做到这一点或改进我的代码吗?

【问题讨论】:

    标签: objective-c uiviewcontroller ios6 background xcode4.5


    【解决方案1】:

    1) 你可以只使用普通的 UIView 而不是没有文本的标签(相同的区别只是更正确)

    2) 在故事板中,UI元素在左侧栏的顺序很重要,基本上是元素的绘制顺序,最上面的先绘制,因此它下面的东西会被绘制结束

    【讨论】:

      【解决方案2】:

      您可以尝试在 Interface Builder 中放置多个 UIImageView 对象,并根据 Attributes Inspector 的需要设置背景,并注意如何将元素放入 UIView 中。下面的那些是在顶部的之后绘制的。

      【讨论】:

        【解决方案3】:

        如果我没听错的话……

        让自己熟悉子视图层次结构背后的逻辑和兄弟的绘制顺序。 在您的界面构建器/故事板中,您可以按如下方式对齐视图:

           UIView (the default view that is connected to the view controller's view property) 
            \--UIView   (This is a background to group UI Items as buttons or Lables
               \-- UILabel
               \-- UIButton
            \--UIView (the next background) 
               \-- UILabel
               \-- UIButton
        

        当谈到坐标时,你会注意到每个视图的坐标都是相对于它的子视图的。因此,UILabel 和 UIButton 可能具有相同的坐标,但它们中的每一个都将位于其子视图中。

        另一种选择是:

        UIView (the default view that is connected to the view controller's view property) 
        \--UIView (Background) 
        \--UIButton
        \--UILable
        \--UIView (Background) 
        \--UIButton
        \--UILable
        

        甚至

        UIView (the default view that is connected to the view controller's view property) 
        \--UIView (Background) 
        \--UIView (Background) 
        \--UIButton
        \--UILable
        \--UIButton
        \--UILable
        

        在这种情况下,所有 UI 元素共享相同的坐标空间。在这种情况下,您需要注意正确的布局。

        这个是行不通的: UIView(连接到视图控制器的视图属性的默认视图) --UI按钮 --UILable --UI按钮 --UILable --UIView(背景) --UIView(背景) 即使所有坐标等与上述示例相同,两个背景视图也会隐藏其他 UI 元素,因为它们是在其他元素之后绘制的。最后一个赢得空间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-30
          • 2016-11-19
          • 1970-01-01
          • 2016-10-05
          相关资源
          最近更新 更多