Ios 使用NSLayoutConstraint编程设计tableview单元
我正在尝试使用NSLayoutControint和可视化格式编程设计一个tableview单元格,而不使用故事板。我已经设法在superview中添加了所有UI元素,但现在需要帮助来安排它们。这是一张照片,我正在努力做的。下面是给出的一段代码。 我在初始化方法中初始化UI元素Ios 使用NSLayoutConstraint编程设计tableview单元,ios,objective-c,xcode,ios7,Ios,Objective C,Xcode,Ios7,我正在尝试使用NSLayoutControint和可视化格式编程设计一个tableview单元格,而不使用故事板。我已经设法在superview中添加了所有UI元素,但现在需要帮助来安排它们。这是一张照片,我正在努力做的。下面是给出的一段代码。 我在初始化方法中初始化UI元素 -(void)initialization { //Defining containerView. UIView *containerView = [UIView new]; containerView.translat
-(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];
}
现在我想使用addcontaints:method以上图所示的相同方式排列所有这些元素。比如说
[captureCallScreenView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-12-[_nameLabel]" options:kNilOptions metrics:nil views:NSDictionaryOfVariableBindings(_nameLabel)]];
感谢您的帮助和建议。为什么需要以编程方式执行此操作?因为我正在研究旧代码。