使用中心NSLayoutConstraint大​​的UIView [英] Center a large UIView using NSLayoutConstraint

查看:100
本文介绍了使用中心NSLayoutConstraint大​​的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来使用自动版式,即使这是我第一次尝试。不管我用它做,我结束与一个白色的屏幕作为结果。这里是我的尝试。

我有一个的UIView ,让我说 parentView (60, 154,200,200)。这是一个子视图 self.view

然后,我有一个动态的观点,说 dynamicView (0,0,260,100)和一个标签(15,25,230,50)这是一个子视图 dynamicView

当我添加了 dynamicView 作为子视图的 parentView ,它超出的parentView 的界限。所以,我想调整的大小 dynamicView 及其子(标签),因此它的位置是中心到 parentView dynamicView 是在 parentView 的界限。

我的第一次尝试设置 clipsToBounds ,它不是在X code5,iOS7工作。因此,下一个选项是实现这一使用 NSLayoutConstraint 。我对它没有任何想法。我欢迎你的想法。


解决方案

我只会解决dyanmicView作为parentView问题的一部分,然后让你从那里

第一:如果您是动态创建视图,那么你是好走,但如果你从故事板创建它,你必须把它从它的父然后重新装这..这是你如何摆脱它的previous NSConstraints(通常故事板推出),可能与你的新的冲突。

您也总得设置它的 setTranslatesAutoresizingMaskIntoConstraints 为NO B / C也可以与您的nsconstraints干扰。

我平时做的那些最后两个步骤,像这样,使用 mapObjectsUsingBlock 使整个繁琐的过程创建约束一点更加愉快和自然的:

  [@ [view_1,view_2,/../,view_n] mapObjectsApplyingBlock:^(UIView的*视图){
        [查看removeFromSuperview]
        [查看setTranslatesAutoresizingMaskIntoConstraints:NO];
        [查看setHidden:NO];
        [上海华addSubview:视图]
    }];

然后申请nsconstraints之前,你必须确保该视图你想要的限制适用于IS的的连接到它的父:

  [parentView addSubview:dynamicView];

然后您想要创建一个绑定字典:

 的NSDictionary * buttonBindingsDictionary = @ {@parentView:parentView,
                                            @dynamicView:dynamicView};

那么你要使用添加的约束<一个href=\"https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html#//apple_ref/doc/uid/TP40010853-CH3\"相对=nofollow>视觉格式语言。我还用 mapObjectsUsingBlock 这里(我会解释英语的每个约束):

 的NSArray * buttonConstraints = [@ [@V:|  -  [dynamicView(大于= 200)]  -  |
                                @| - [dynamicView(大于= 260)〕 - |,
                                ] mapObjectsUsingBlock:^ ID(的NSString * formatString中,NSUInteger IDX){
    返回[NSLayoutConstraint constraintsWithVisualFormat:formatString的选项:0的指标:无意见:buttonBindingsDictionary];
}];

五:| - [dynamicView(大于= 200)] - | 意味着垂直讲.. dynamicView之间的上下距离和它的父母应该是平等的。也 dynamicView的的高度应不小于 200

| - [dynamicView(大于= 260)] - | 意味着水平来讲..之间的左右距离 dyanmicview 和它的父母应该是平等的。也 dyanmicView的宽度应不大于 260

注:的你可以做自己的数学和设置到底有多少 dyanicView 之间的左/右/底/顶的距离和它的父..这仅仅是简单的..但有时nsconstraints混乱起来,我得做我自己。

在这种情况下它会是这个样子,其中 X 是你想出的距离:

 五:| -X- [dynamicView(大于= 200)]  -  X- |
| -X- [dynamicView(大于= 260)〕 - x坐标|

然后你必须约束添加到父视图:

  [parentView addConstraints:[buttonConstraints flattenArray]];

在这里,我使用通知扁平化阵列,再次这是从我的图书馆B / CI希望的方法给它一个级别阵列,而不是一个数组的数组。

和你去好!

注意:我知道这可能并不完美..但它可以让你做什么+一些辅助文件的想法。它需要一些练习,你应该看看一些的教程的肯定..我建议你开始用故事板nsconstraints(你可以选择一个视图..然后去编辑>针> ..然后选择东西..获得一些直接的视觉反馈..你也可以模拟一下你的观点会像在3.5显示器VS 4.0立即显示在情节提要通过选择视图控制器,然后转到属性检查器和模拟指标下选择不同的大小)

你慢慢萌芽,但有一点可以肯定:一旦你nsconstraints你将永远不会回头!这是完全值得的!

P.S。你也可以使用意见 nsconstraints以及..以防万一你想知道。

I am new to use Autolayouts, even this is my first try. Whatever I do with it, I end with a white screen as result. Here is my attempt.

I have a UIView, let me say a parentView of frame (60, 154, 200, 200). It is a subview to self.view.

Then I have a dynamic view, say dynamicView of frame (0, 0, 260, 100) and a label of frame (15, 25, 230, 50) which is a subview to dynamicView.

When I add the dynamicView as subview to the parentView, it goes out of parentView's bounds. So, I'd like to adjust the size of the dynamicView and its child(label) so that its position is center to the parentView and dynamicView is inside parentView's bounds.

My first attempt was setting clipsToBounds, its not working in Xcode5, iOS7. So the next option is to achieve this using NSLayoutConstraint. I have no idea on it. I welcome your ideas.

解决方案

i'll just address the dyanmicView being part of the parentView issue, then let you go from there

first: if you are creating the view dynamically, then you're good to go, but if you've created it from storyboard, you'd have to detach it from it's parent then reattach it.. that's how you get rid of it's previous NSConstraints (that usually the storyboard introduces) that may conflict with your new ones.

you also gotta set it's setTranslatesAutoresizingMaskIntoConstraints to NO b/c that also can interfere with your nsconstraints.

I usually do those last two steps like so, using mapObjectsUsingBlock to make the whole tedious process of creating constraints a bit more pleasant and natural:

    [@[view_1, view_2, /../, view_n] mapObjectsApplyingBlock:^(UIView *view) {
        [view removeFromSuperview];
        [view setTranslatesAutoresizingMaskIntoConstraints:NO];
        [view setHidden:NO];
        [superView addSubview:view];
    }];

then before applying nsconstraints, you gotta make sure that the view you want the constraints to apply to is already attached to it's parent:

[parentView addSubview:dynamicView];

then you want to create a bindings dictionary:

NSDictionary *buttonBindingsDictionary = @{ @"parentView" : parentView,
                                            @"dynamicView" : dynamicView};

then you want to add the constraints using the visual format language.. I also use mapObjectsUsingBlock here (i'll explain each constraint in english):

NSArray *buttonConstraints = [@[@"V:|-[dynamicView(>=200)]-|",
                                @"|-[dynamicView(>=260)]-|",
                                ] mapObjectsUsingBlock:^id(NSString *formatString, NSUInteger idx){
    return [NSLayoutConstraint constraintsWithVisualFormat:formatString options:0 metrics:nil views:buttonBindingsDictionary];
}];

V:|-[dynamicView(>=200)]-| means that vertically speaking.. the upper and lower distance between dynamicView and it's parent should be equal.. also dynamicView's height should be no less than 200

|-[dynamicView(>=260)]-| means that horizontally speaking.. the left and right distance between dyanmicview and it's parent should be equal.. also dyanmicView's width should be no less than 260

note: you can do the math yourself and set exactly how much the left/right/bottom/top distance between dyanicView and it's parent.. this is just simpler.. but sometimes nsconstraints messes up and I gotta do it myself.

in that case it would look something like this where x is the distance you came up with:

V:|-x-[dynamicView(>=200)]-x-|
|-x-[dynamicView(>=260)]-x-|

then you gotta add the constraints to the parent view:

[parentView addConstraints:[buttonConstraints flattenArray]];

notice here i used flatten array, again that's a method from my library b/c i want to feed it a one level array, not an array of arrays.

and you're good to go!

note: i know that this may not work perfectly.. but it gives you the idea of what to do + some helper files. It takes some practice, and you should look at some of the tutorials for sure.. i suggest you start with nsconstraints using storyboard (you can select a view.. then go editor>pin>.. then chose something.. get some immediate visual feedback.. also you can simulate what your views will look like in 3.5" displays vs 4.0" displays immediately on the storyboard by selecting the view controller, then going to attributes inspector and selecting different sizes under simulated metrics)

take your time bud, but one thing for sure: once you go nsconstraints you will never look back! it's totally worth it!

p.s. you can also animate views using nsconstraints as well.. just in case you were wondering.

这篇关于使用中心NSLayoutConstraint大​​的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆