简体   繁体   English

如何使用不同屏幕的约束来调整视图的高度和宽度?

[英]How to resize height & width of the view using constraints for different screen?

I am very new to xocde 6 & autolayout.I am designing screens for multilple devices.I have some views such as images,textfields.I have desgined them for iPhone.I have added some fix height & width constraints.So height & width remian fix for all devices.I want to change the height & width of the view acrding to screen size.I have tried one method which was 我是xocde 6和autolayout的新手。我正在设计用于多种设备的屏幕。我有一些视图,例如图像,文本字段。我已经将它们设计用于iPhone。我添加了一些固定的高度和宽度约束。修复所有设备。我要根据屏幕尺寸更改视图的高度和宽度。我尝试了一种方法

  1. make width equal to parent view & give multiplier 0.5. 使宽度等于父视图的宽度并乘以0.5。
  2. keep height fix. 保持高度固定。
  3. give aspect ratio. 给出宽高比。

This way view was aligned but it height was fixed.I have also used leading space,trailing space but in this way sometimes unexpected results are achieved.So is there any better way of doing it? 这种方式视图是对齐的,但高度是固定的。我也使用过引导空间,尾部空间,但有时这样会产生意想不到的结果。那么还有更好的方法吗? I am really having hard time figuring it out. 我真的很难解决这个问题。

If you're new, just for alternative I suggest you to use PureLayout. 如果您是新手,我建议您使用PureLayout作为替代。 You can use in your project with cocoapods or Carthage. 您可以将cocoapods或Carthage用于项目中。 With this library you can super easily make this constraints for different screens. 使用此库,您可以非常轻松地针对不同的屏幕设置此约束。 There is a example project in github project where you can find many different scenarios in. Here is the github page of project : github项目中有一个示例项目,您可以在其中找到许多不同的场景。这是project的github页面:

https://github.com/smileyborg/PureLayout https://github.com/smileyborg/PureLayout

Here is some example code snippet for you @deepak kumar : 这是@deepak kumar的一些示例代码片段:

- (void)updateViewConstraints
{
    if (!self.didSetupConstraints) {
        // Apply a fixed height of 50 pt to two views at once, and a fixed height of 70 pt to another two views
        [@[self.redView, self.yellowView] autoSetViewsDimension:ALDimensionHeight toSize:50.0];
        [@[self.blueView, self.greenView] autoSetViewsDimension:ALDimensionHeight toSize:70.0];

        NSArray *views = @[self.redView, self.blueView, self.yellowView, self.greenView];

        // Match the widths of all the views
        [views autoMatchViewsDimension:ALDimensionWidth];

        // Pin the red view 20 pt from the top layout guide of the view controller
        [self.redView autoPinToTopLayoutGuideOfViewController:self withInset:20.0];

        // Loop over the views, attaching the left edge to the previous view's right edge,
        // and the top edge to the previous view's bottom edge
        [[views firstObject] autoPinEdgeToSuperviewEdge:ALEdgeLeft];
        UIView *previousView = nil;
        for (UIView *view in views) {
            if (previousView) {
                [view autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:previousView];
                [view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:previousView];
            }
            previousView = view;
        }
        [[views lastObject] autoPinEdgeToSuperviewEdge:ALEdgeRight];

        self.didSetupConstraints = YES;
    }

    [super updateViewConstraints];
}

I believe if you could start now, it'll be easier for you to pass auto layout headache. 我相信,如果您现在就可以开始,这将使您轻松克服自动布局难题。

I'm not sure what you're trying to ask, but it sounds like an Auto Layout question, if you don't want your Text View to fill up the entire screen, go ahead and select the Text View in your Main.storyboard and click the Pin button, like so: 我不确定您要问的是什么,但这听起来像是“ 自动布局”问题,如果您不希望“ 文本视图”填满整个屏幕,请继续在Main.storyboard中 选择文本视图”然后单击“ 固定”按钮,如下所示:

在此处输入图片说明

Notice that all of the constraints are enabled, which means this text view will pin to the edges of your iOS screen. 请注意,所有约束都已启用,这意味着此文本视图将固定在iOS屏幕的边缘。

If you want to later edit those constrains, go ahead and go to the Size Inspector , and you can directly click on the constrains: 如果以后要编辑这些约束,请继续并转到Size Inspector ,您可以直接单击约束:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们使用不同大小和类型的屏幕时,如何在iOS中设置UI控件的宽度和高度限制? - How setting width and height constraints of UI controls in iOS works when we use different size and types of screen? 如何为指定的屏幕尺寸提供单独的高度和宽度约束,例如为iPhone和iPad提供不同的约束? - how to give separate height and width constraint for specified screen size for eg giving different constraints for iphone and ipad? 使用自动布局约束时如何获取视图的当前宽度和高度? - How can I get a view's current width and height when using autolayout constraints? 如何在 SwiftUI 中获取视图或屏幕的高度和宽度 - How to get Height and Width of View or Screen in SwiftUI 如何使用约束设置自视高度? - How to set self view height using constraints? 如何根据手机使用限制来减小图像的宽度和高度? - How to decrease the image width and height depend upon phone using constraints? 是否可以调整金属视图(MTKView())的大小,使其高度延伸到整个屏幕但保持宽度的比例? - Is it possible to resize a metal view (MTKView()) so its height stretches out the whole screen but the ratio to the width is maintained? 具有不同高度和宽度计算的视图 - View with different height and width calculation 如何根据不同的屏幕尺寸(高度和宽度)调整子视图? - How to adjust Subview according to different Screen Sizes (Height and Width)? 启动屏幕XIB:缺少宽度/高度限制(Xcode 6) - Launch Screen XIB: Missing Width / Height Constraints (Xcode 6)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM