Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自动布局和约束问题_Ios_Objective C_Uiview_Uiviewcontroller_Autolayout - Fatal编程技术网

Ios 自动布局和约束问题

Ios 自动布局和约束问题,ios,objective-c,uiview,uiviewcontroller,autolayout,Ios,Objective C,Uiview,Uiviewcontroller,Autolayout,我对iOS和AutoLayout有问题…这是一个测试应用程序,我在其中复制了这个问题。它包含一个简单的视图控制器,其中包含一个固定的ui按钮;当用户单击此按钮时,代理必须创建自定义视图,应用其定位约束;然后,视图有一个方法来构建其内容视图,并且子视图也带有约束 代码如下: //MyViewController.m @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any ad

我对iOS和AutoLayout有问题…这是一个测试应用程序,我在其中复制了这个问题。它包含一个简单的视图控制器,其中包含一个固定的
ui按钮
;当用户单击此按钮时,代理必须创建自定义视图,应用其定位约束;然后,视图有一个方法来构建其内容视图,并且子视图也带有约束

代码如下:

//MyViewController.m

@implementation MyViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

  UIButton *start_but = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
  [start_but setTitle:@"Draw" forState:UIControlStateNormal];
  [start_but setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
  [start_but addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];

  [self.view addSubview:start_but];
}

- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

- (void)clickAction: (id)sender
{

  localView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 400, 300)];

  UIView *superview = (UIView*)localView;

  superview.translatesAutoresizingMaskIntoConstraints = NO;

  [self.view addSubview:localView];

  NSLayoutConstraint *constr = [NSLayoutConstraint constraintWithItem:localView
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:100.0];

  [self.view addConstraint:constr];

  constr = [NSLayoutConstraint constraintWithItem:localView
                                                          attribute:NSLayoutAttributeRight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeRight
                                                         multiplier:1.0
                                                           constant:-50.0];
  [self.view addConstraint:constr];



  [localView CreateGuiInterface];
}

@end


//MyView.m


@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self)
  {
    // Initialization code



  }
  return self;
}

-(void)CreateGuiInterface
{
  UIView *superview = self;

  self.backgroundColor = [UIColor redColor];

  UIButton *but1 = [[UIButton alloc] init];
  [but1 setTitle:@"Button" forState:UIControlStateNormal];
  [but1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  but1.backgroundColor = [UIColor yellowColor];

  but1.translatesAutoresizingMaskIntoConstraints = NO;

  [superview addSubview:but1];

  NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:but1
                                                              attribute:NSLayoutAttributeRight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:superview
                                                              attribute:NSLayoutAttributeRight multiplier:1.0
                                                               constant:-20.0];

  [superview addConstraint:constraint];

  constraint = [NSLayoutConstraint constraintWithItem:but1
                                          attribute:NSLayoutAttributeTop
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:superview
                                          attribute:NSLayoutAttributeTop
                                         multiplier:1.0
                                           constant:50.0];

  [superview addConstraint:constraint];

  UILabel *label = [[UILabel alloc] init];

  label.text = @"Label";
  label.backgroundColor = [UIColor greenColor];
  label.textColor = [UIColor blackColor];
  label.translatesAutoresizingMaskIntoConstraints = NO;

  [superview addSubview:label];

  constraint = [NSLayoutConstraint constraintWithItem:label
                                          attribute:NSLayoutAttributeRight
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:but1
                                          attribute:NSLayoutAttributeLeft
                                         multiplier:1.0
                                           constant:-40.0];
  [superview addConstraint:constraint];

  constraint = [NSLayoutConstraint constraintWithItem:label
                                          attribute:NSLayoutAttributeTop
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:but1
                                          attribute:NSLayoutAttributeBottom
                                         multiplier:1.0
                                           constant:20.0];

  [superview addConstraint:constraint];

} 

@end
所以,问题是当我单击iOS时,似乎无法绘制视图背景色。这很奇怪,因为如果我不使用
superview.translatesAutoresizingMaskIntoConstraints=NO
并将视图放置在固定位置(例如使用
initWithFrame:CGRect(100200300400)
),而不使用约束,它工作正常:在子视图和父视图中使用约束会有问题吗?阿普雷多克说,这些约束不能通过整个视图屏障;因此,我编写了带有本地视图约束的应用程序。。。。 有人能帮我吗


感谢您的建议

使用autoLayout的目的是永远不必在任何视图上设置框架。如果希望以编程方式将视图放置在superview中,则将为此提供约束。我所做的更改将使您看到预期的红色背景。我没有更改MyView实现(只是添加了-(void)CreateGUI接口; 在MyView.h中)。我为演示所做的更改在MyViewController实现中

尝试使用以下代码代替viewController:

@interface MyViewController ()
@property (nonatomic, strong) MyView* localView;
@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIButton *start_but = [[UIButton alloc] init];
    start_but.translatesAutoresizingMaskIntoConstraints = NO;

    [start_but setTitle:@"Draw" forState:UIControlStateNormal];
    [start_but setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [start_but addTarget:self action:@selector(clickAction:)  forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:start_but];

    // The following constraints simply place the initial start button in the top left
    NSDictionary* views = NSDictionaryOfVariableBindings(start_but);
    NSString* format = @"H:|-[start_but]";
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];
    [self.view addConstraints:constraints];
    format = @"V:|-[start_but]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                          options:0
                                                          metrics:nil
                                                            views:views];
    [self.view addConstraints:constraints];
}

- (void)clickAction: (id)sender
{
    self.localView = [[MyView alloc] init];

    UIView *superview = (UIView*)self.localView;

    superview.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:self.localView];

    // Here I'm placing your parent view (MyView) 50 points on right of the superview with
    // a width of 400 points
    NSDictionary* views = NSDictionaryOfVariableBindings(superview);
    NSString* format = @"H:[superview(==400)]-(50)-|";
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];
    [self.view addConstraints:constraints];

    // Vertically 100 points from the top of the superview with a height of 300 points
    format = @"V:|-(100)-[superview(==300)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                          options:0
                                                          metrics:nil
                                                            views:views];
    [self.view addConstraints:constraints];

    [self.localView CreateGuiInterface];
}

@end

是的,你在写,很有效,非常感谢。所以,问题是我用initWithFrame设置框架大小…将视图大小设置为约束解决了我的问题。再次感谢!