无法更改从xib加载的UIView的大小和位置

时间:2022-08-27 10:50:48

I have a UICollectionView and the dark grey area on the top. In this grey area I needed to put tabs with UIView. Because I'll need outlets, this UIView has its own TabsViewController. No matter what I do, the view I am trying to add is always stays in the top left corner with strange offset and size.

我有一个UICollectionView和顶部的深灰色区域。在这个灰色区域,我需要用UIView标记。因为我需要插座,所以这个UIView有自己的TabsViewController。无论我做什么,我想要添加的视图始终位于左上角,具有奇怪的偏移和大小。

I tried to change it size and position or to put it on top of different views — it is always gives the same visual result.

我试图改变它的大小和位置或者把它放在不同的视图之上 - 它总是给出相同的视觉效果。

Auto Layout is on. This view should have height of 72 and 100% width.

自动布局已开启。此视图的高度应为72和100%。

无法更改从xib加载的UIView的大小和位置

TabsViewController *tabs = [[TabsViewController alloc] init];
CGSize screen = // returns correct screen size
tabs.view.frame = CGRectMake(0, 0, screen.width, 72);
[self.collectionView addSubview:tabs.view];

I tried to call code above from multiple places, including viewDidLoad, viewWillAppear, viewDidLayoutSubviews and viewDidAppear.

我试图从多个地方调用上面的代码,包括viewDidLoad,viewWillAppear,viewDidLayoutSubviews和viewDidAppear。

1 个解决方案

#1


In the auto layout you have to change the constrains instead of frame. Just connect your view constrains to code as and try changing your constrains to set your view programatically.

在自动布局中,您必须更改约束而不是框架。只需将视图约束作为nslayoutconstrains连接到代码,然后尝试更改约束以编程方式设置视图。

Connect constraint of your view in IB to code as IBOutlet properties

将IB中视图的约束连接到代码作为IBOutlet属性

Example :

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hieght;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *horizontalCenter;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verticalCenter;

And change there values as:

并将那些值更改为:

self.width.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.hieght.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.topSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.horizontalCenter.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.leftSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.rightSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.verticalCenter.constant=CURRENT_VALUE_FOR_CONSTRAINT;

无法更改从xib加载的UIView的大小和位置无法更改从xib加载的UIView的大小和位置

#1


In the auto layout you have to change the constrains instead of frame. Just connect your view constrains to code as and try changing your constrains to set your view programatically.

在自动布局中,您必须更改约束而不是框架。只需将视图约束作为nslayoutconstrains连接到代码,然后尝试更改约束以编程方式设置视图。

Connect constraint of your view in IB to code as IBOutlet properties

将IB中视图的约束连接到代码作为IBOutlet属性

Example :

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hieght;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *horizontalCenter;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verticalCenter;

And change there values as:

并将那些值更改为:

self.width.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.hieght.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.topSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.horizontalCenter.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.leftSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.rightSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.verticalCenter.constant=CURRENT_VALUE_FOR_CONSTRAINT;

无法更改从xib加载的UIView的大小和位置无法更改从xib加载的UIView的大小和位置