使用NSLayoutConstraint

时间:2015-09-24 05:24:37

标签: ios objective-c xcode ios7

我正在尝试使用具有可视格式的NSLayoutContraint以编程方式设计tableview单元格而不使用storyboard。我已设法在superview中添加所有UI元素,但现在需要帮助来安排它们。这是一张图片,我正在尝试做什么。下面是给出的代码。  This is my tableview cell      我在初始化方法中初始化UI元素。

-(void)initialization {

//Defining containerView.
UIView *containerView = [UIView new];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:containerView];

NSDictionary *metrics = @{ @"padding" : @10 };
NSDictionary *viewsI = NSDictionaryOfVariableBindings(containerView);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-padding-[containerView]-padding-|" options:kNilOptions metrics:metrics views:viewsI]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:kNilOptions metrics:metrics views:viewsI]];

//Defining captureCallScreenView as superview.
UIView *captureCallScreenView = [UIView new];
captureCallScreenView.backgroundColor = [UIColor int_grayIIColor];
captureCallScreenView.translatesAutoresizingMaskIntoConstraints = NO;

UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
                                        action:@selector(handleSingleTap:)];
[captureCallScreenView addGestureRecognizer:singleFingerTap];
[containerView addSubview:captureCallScreenView];

//Defining CaptureCall Screen elements such as name, number, status, timedate labels and two buttons as shown in above picture.
self.nameLabel = [UILabel new];
self.nameLabel.textColor = [UIColor int_blackColorClear];
self.nameLabel.font = [UIFont appFontOfType:AppFontRegular size:14];
self.nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
[captureCallScreenView addSubview:self.nameLabel];

self.numberLabel = [UILabel new];
self.numberLabel.font = [UIFont appFontOfType:AppFontRegular size:12];
self.numberLabel.textColor = [UIColor int_darkGrayColor];
self.numberLabel.translatesAutoresizingMaskIntoConstraints = NO;
[captureCallScreenView addSubview:self.numberLabel];

self.callStatusLabel = [UILabel new];
self.callStatusLabel.font = [UIFont appFontOfType:AppFontRegular size:12];
self.callStatusLabel.textColor = [UIColor int_darkGrayColor];
self.callStatusLabel.translatesAutoresizingMaskIntoConstraints = NO;
[captureCallScreenView addSubview:self.callStatusLabel];

self.interactionTimeLabel = [UILabel new];
self.interactionTimeLabel.font = [UIFont appFontOfType:AppFontRegular size:12];
self.interactionTimeLabel.textColor = [UIColor int_darkGrayColor];
self.interactionTimeLabel.translatesAutoresizingMaskIntoConstraints = NO;
[captureCallScreenView addSubview:self.interactionTimeLabel];

self.interactionDateLabel = [UILabel new];
self.interactionDateLabel.font = [UIFont appFontOfType:AppFontRegular size:12];
self.interactionDateLabel.textColor = [UIColor int_darkGrayColor];
self.interactionDateLabel.translatesAutoresizingMaskIntoConstraints = NO;
[captureCallScreenView addSubview:self.interactionDateLabel];

self.callDuration = [UILabel new];
self.callDuration.font = [UIFont appFontOfType:AppFontRegular size:12];
self.callDuration.textColor = [UIColor int_darkGrayColor];
self.callDuration.translatesAutoresizingMaskIntoConstraints = NO;
[captureCallScreenView addSubview:self.callDuration];

self.CaptureOk = [UIButton buttonWithType:UIButtonTypeCustom];
[self.CaptureOk setImage:[UIImage imageNamed:@"checkmark.png"] forState:UIControlStateNormal];
[self.CaptureOk addTarget:self action:@selector(CaptureOkTap:) forControlEvents:UIControlEventTouchUpInside];
self.CaptureOk.frame = CGRectMake(0,0,BUTTON_WIDTH_HEIGHT,BUTTON_WIDTH_HEIGHT);
self.clipsToBounds = YES;
self.CaptureOk.layer.cornerRadius = BUTTON_WIDTH_HEIGHT/2.0f;
self.CaptureOk.layer.borderColor=[UIColor int_orangeColorDark].CGColor;
self.CaptureOk.layer.borderWidth=2.0f;
[captureCallScreenView addSubview:self.CaptureOk];

self.CaptureCancel = [UIButton buttonWithType:UIButtonTypeCustom];
[self.CaptureCancel setImage:[UIImage imageNamed:@"close_orange.png"] forState:UIControlStateNormal];
[self.CaptureCancel addTarget:self action:@selector(CaptureCancelTap:) forControlEvents:UIControlEventTouchUpInside];
self.CaptureCancel.frame = CGRectMake(0,0,BUTTON_WIDTH_HEIGHT,BUTTON_WIDTH_HEIGHT);
self.clipsToBounds = YES;
self.CaptureCancel.layer.cornerRadius = BUTTON_WIDTH_HEIGHT/2.0f;
self.CaptureCancel.layer.borderColor=[UIColor int_orangeColorDark].CGColor;
self.CaptureCancel.layer.borderWidth=2.0f;
[captureCallScreenView addSubview:self.CaptureCancel];
}

现在我想使用addContraints:method以与上图所示相同的方式排列所有这些元素。例如。

 [captureCallScreenView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-12-[_nameLabel]" options:kNilOptions metrics:nil views:NSDictionaryOfVariableBindings(_nameLabel)]];

感谢您的帮助和建议。

0 个答案:

没有答案