Objective-C add more UIButton horizontally - ios

I am using a open source UIAlertView.View is
I want to add more button like horizontally, one in left side and another in right of BYE button, like this one
my using source-code are bellow
-(void)popUPView{
UIView* contentView = [[UIView alloc] init];
contentView.translatesAutoresizingMaskIntoConstraints = NO;
contentView.backgroundColor = [UIColor klcLightGreenColor];
contentView.layer.cornerRadius = 12.0;
UILabel* dismissLabel = [[UILabel alloc] init];
dismissLabel.translatesAutoresizingMaskIntoConstraints = NO;
dismissLabel.backgroundColor = [UIColor clearColor];
dismissLabel.textColor = [UIColor whiteColor];
dismissLabel.font = [UIFont boldSystemFontOfSize:32.0];
dismissLabel.text = #"Hi.";
UIButton* dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
dismissButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
dismissButton.backgroundColor = [UIColor klcGreenColor];
[dismissButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[dismissButton setTitleColor:[[dismissButton titleColorForState:UIControlStateNormal] colorWithAlphaComponent:0.5] forState:UIControlStateHighlighted];
dismissButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
[dismissButton setTitle:#"Bye" forState:UIControlStateNormal];
dismissButton.layer.cornerRadius = 6.0;
[contentView addSubview:dismissLabel];
[contentView addSubview:dismissButton];
NSDictionary* views = NSDictionaryOfVariableBindings(contentView, dismissButton, dismissLabel);
[contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(16)-[dismissLabel]-(10)-[dismissButton]-(24)-|"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views]];
[contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(36)-[dismissLabel]-(36)-|"
options:0
metrics:nil
views:views]];
KLCPopup *popup = [KLCPopup popupWithContentView:contentView
showType:KLCPopupShowTypeShrinkIn
dismissType:KLCPopupDismissTypeShrinkOut
maskType:KLCPopupMaskTypeDimmed
dismissOnBackgroundTouch:TRUE
dismissOnContentTouch:FALSE];
[popup show];
}
would you kindly help me how to solve this problem . Thanks is advance

For this you must set proper leading/trailing values ,
leftView.trailing = rightView.leading
for example if you want to align a new view to the right , you would do something like this
UIView *viewToAllign = yourView;
UIView *view = [[UIView alloc] init];
imageViewOn.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view];
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:viewToAllign
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:SPACING_BETWEEN_VIEWS];
or if you want to align something on the left , just switch layout attributes
UIView *viewToAllign = yourView;
UIView *view = [[UIView alloc] init];
imageViewOn.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view];
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:viewToAllign
attribute:NSLayoutAttributeLeading
multiplier:1
constant:SPACING_BETWEEN_VIEWS];
then you have to create other constraints ( height , width , top and bottom )
after this , you can add constraints to your container view

Related

How to have both UILabel and UIButton items into a UIView for UITableView header section

This is my first post here, so I hope to do everything correctly.
I'm trying to write a code in Objective-C that allows me to add a UIButton in a table header view, for a specific section.
This is how it should look:
I see correctly the title, but unfortunately the UIButton doesn't appear and I don't know why.. some months ago I've used a similar code and it worked, but after changing something due to another bug in the table view, it stopped working and unfortunately I haven't the original code anymore..
Do you know where I'm wrong or do you know any alternative?
This is the code in Objective-C (that should automatically align the title and the button, even when the user changes the device orientation and it should work for any screen size):
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 2) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 0,0)];
[view setClipsToBounds:NO];
[view setPreservesSuperviewLayoutMargins:TRUE];
UILabel *textLabel = [[UILabel alloc] init];
textLabel.text = #"MY TITLE";
textLabel.textColor = [UIColor colorWithRed:0.427f green:0.427f blue:0.427f alpha:1.0f];
//[textLabel setFont:_tableViewFooterFont];
[textLabel setTextAlignment:4]; // Natural
[textLabel setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[textLabel setAccessibilityTraits:UIAccessibilityTraitHeader];
[view addSubview:textLabel];
UIButton *button = [UIButton buttonWithType:1];
//[button setHidden:FALSE];
//[button.titleLabel setFont:_tableViewFooterFont];
[button.titleLabel setTextColor:[UIColor redColor]];
[button setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[button setTitle:#"RESET" forState:0];
[button setContentCompressionResistancePriority:0 forAxis:UILayoutConstraintAxisHorizontal];
[button addTarget:self action:#selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
NSDictionary *dictionary = NSDictionaryOfVariableBindings(textLabel, button);
NSLayoutConstraint *buttonConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"|-[textLabel]->=0-[button]-|" options:0 metrics:NULL views:dictionary];
[view addConstraints:buttonConstraint];
NSLayoutConstraint *labelConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-12-[textLabel]-6-|" options:0 metrics:NULL views:dictionary];
[view addConstraints:labelConstraint];
NSLayoutConstraint *viewConstraint = [NSLayoutConstraint constraintWithItem:button attribute:10 relatedBy:0 toItem:textLabel attribute:10 multiplier:1.0 constant:0];
[view addConstraint:viewConstraint];
return view;
}
return NULL;
}
Try this:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create view for header
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)]; // set header view height as you want
headerView.backgroundColor = [UIColor clearColor] ;
// create label for header title
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width-130, 30)];
title.font = [UIFont fontWithName:#"FONT_NAME" size:16.0]; //optional
[title.heightAnchor constraintEqualToConstant:30].active = true;
[title.widthAnchor constraintEqualToConstant:tableView.frame.size.width-130].active = true;
// create button for header action
UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(self.yourTableView.frame.size.width-120, 0, 120, 30)];
[resetButton setTitle:#"RESET" forState:UIControlStateNormal];
//resetButton.backgroundColor = [UIColor redColor];
[resetButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[resetButton setFont:[UIFont fontWithName:#"FONT_NAME" size:16.0]] ;
[resetButton.heightAnchor constraintEqualToConstant:30].active = true;
[resetButton.widthAnchor constraintEqualToConstant:120].active = true;
//Stack View
UIStackView *stackView = [[UIStackView alloc] initWithFrame:headerView.frame];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFillProportionally;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 10;
[stackView addArrangedSubview:title];
[stackView addArrangedSubview:resetButton];
stackView.translatesAutoresizingMaskIntoConstraints = false;
[headerView addSubview:stackView];
if(section == 2){
title.text = #"MY TITLE";
[resetButton addTarget:self action:#selector(resetBtnActn:) forControlEvents:UIControlEventTouchUpInside] ;
}else{
headerView.hidden = YES ;
title.hidden = YES ;
resetButton.hidden = YES ;
}
//Trailing
NSLayoutConstraint *trailing =[NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.f];
//Leading
NSLayoutConstraint *leading = [NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeLeadingMargin
multiplier:1.0f
constant:0.f];
//Bottom
NSLayoutConstraint *bottom =[NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.f];
//Add constraints
[headerView addConstraint:trailing];
[headerView addConstraint:bottom];
[headerView addConstraint:leading];
// 5. Finally return
return headerView;
}
// table section button action
-(IBAction)resetBtnActn:(id)sender
{
NSLog(#"button tapped");
}
comment if you have any issue.

Objective C: Center with NSLayoutConstraint

NSArray *imageViewHorConst2 = [NSLayoutConstraint constraintsWithVisualFormat:#"H:[_startbutton(50)]-30-[_resetbutton(50)]" options:0 metrics:nil views:viewsDictionary2];}
I'm trying to get those 2 buttons (startbutton, resetbutton) to be separated by some value and be centred.
I'm not sure how to do this in objective C
Here is the snippet to add the two buttons in the center of the screen separated by 30 pixels:
UIButton *button1 = [[UIButton alloc] init];
button1.translatesAutoresizingMaskIntoConstraints = FALSE;
[button1 setTitle:#"Button 1" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor blueColor];
UIButton *button2 = [[UIButton alloc] init];
button2.translatesAutoresizingMaskIntoConstraints = FALSE;
[button2 setTitle:#"Button 2" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor brownColor];
// Add a container view to hold two buttons
UIView *containerView = [[UIView alloc] init];
containerView.translatesAutoresizingMaskIntoConstraints = FALSE;
[containerView addSubview:button1];
[containerView addSubview:button2];
// vertical constraints
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[button1]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button1)]];
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[button2]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button2)]];
// horizontal constraints
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[button1]-(30)-[button2]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button1, button2)]];
// Add the container view with centerx and centery constraints to
// keep the buttons in the center
[self.view addSubview:containerView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
Screenshot:
You can easily accomplish this with UIStackView
self.view.backgroundColor = [UIColor redColor];
UIStackView*st = [UIStackView new];
st.distribution = UIStackViewDistributionFillEqually;
st.axis = UILayoutConstraintAxisHorizontal;
st.spacing = 40;
UIButton*b1 = [UIButton buttonWithType:UIButtonTypeSystem];
UIButton*b2 = [UIButton buttonWithType:UIButtonTypeSystem];
[b1 setTitle:#"ClickB1" forState:UIControlStateNormal];
[b2 setTitle:#"ClickB2" forState:UIControlStateNormal];
st.translatesAutoresizingMaskIntoConstraints = NO;
b1.translatesAutoresizingMaskIntoConstraints = NO;
b2.translatesAutoresizingMaskIntoConstraints = NO;
[st addArrangedSubview:b1];
[st addArrangedSubview:b2];
[self.view addSubview:st];
[[st.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor] setActive:YES];
[[st.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor] setActive:YES];

how to make rectangle for imageView frame as a subview in objective c

how to make rectangle for imageView frame as a subview in objective c?
inside view controller i've a view , inside that view i want to put an imageView , and imageView frame should be less than the frame of the view frame and imageView should be in the centre of the view frame.
Firstly, set the imageView's size, secondary, set the imageView's coordinates, here is code:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
container.backgroundColor = [UIColor blueColor];
[self.view addSubview:container];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.backgroundColor = [UIColor redColor];
[container addSubview:imageView];
CGRect f = imageView.frame;
f.origin.x = CGRectGetWidth(container.bounds) / 2.0f - CGRectGetWidth(f) / 2.0;
f.origin.y = CGRectGetHeight(container.bounds) / 2.0f - CGRectGetHeight(f) / 2.0f;
imageView.frame = f;
or set the imageView's center(Alert! The imageView add to the self.view!)
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
container.backgroundColor = [UIColor blueColor];
[self.view addSubview:container];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
imageView.center = container.center;
Also, you can use autolayout, like this:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
container.backgroundColor = [UIColor blueColor];
[self.view addSubview:container];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor redColor];
[container addSubview:imageView];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *c1 = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100];
NSLayoutConstraint *c2 = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100];
NSLayoutConstraint *c3 = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:container attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint *c4 = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:container attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
c1.active = YES;
c2.active = YES;
c3.active = YES;
c4.active = YES;
All of the solutions above will put the imageView at the center of container

UILabel is not centered in my UIView

I'm adding a UILabel to a UIView on initialization and the label will not center itself. I've tried centering it is layoutSubviews and adding constraints programmatically with no luck. When it appears it is about 200 pixels to the left of the center. Here is my code. Thanks in advance.
- (instancetype)initWithTitle:(NSString *)title andBody:(NSString *)body Andwidth:(CGFloat)width {
self = [super initWithFrame:CGRectMake(0, 0, width, 60)];
if (self) {
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 60)];
containerView.backgroundColor = [UIColor orangeColor];
[self addSubview:containerView];
self.winningLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
self.winningLabel.textColor = [UIColor whiteColor];
self.winningLabel.font = [UIFont fontWithName:#"Avenir" size:30];
self.winningLabel.backgroundColor = [UIColor clearColor];
self.winningLabel.lineBreakMode = NSLineBreakByTruncatingTail;
self.winningLabel.numberOfLines = 2;
self.winningLabel.text = #"YOU WIN";
self.winningLabel.hidden = YES;
[containerView addSubview:self.winningLabel];
NSLayoutConstraint *xCenterConstraint = [NSLayoutConstraint constraintWithItem:self.winningLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
[self addConstraint:xCenterConstraint];
NSLayoutConstraint *yCenterConstraint = [NSLayoutConstraint constraintWithItem:self.winningLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
[self addConstraint:yCenterConstraint];
}
return self;
}
- (void)layoutSubviews {
[self.winningLabel setCenter:self.center];
}
I think you forget the text alignment. By default the text in a label is aligned to the left. If you want it to be in the center, you should specify it explicitly.
self.winningLabel.textAlignment = NSTextAlignmentCenter;

adding NSLayoutConstraint makes views disappear

I have a simple view (controller) with two subviews:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *viewA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewA.backgroundColor = [UIColor redColor];
[self.view addSubview:viewA];
UIView *viewB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
viewB.translatesAutoresizingMaskIntoConstraints = NO;
viewB.backgroundColor = [UIColor grayColor];
[self.view addSubview:viewB];
}
Of course they are overlapping at this point (i can only see viewB). So I add a constraint to make viewB go below viewA, like this:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:viewB
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:viewA
attribute:NSLayoutAttributeBottom
multiplier:1.f
constant:0]];
This makes both views disappear. What am I doing wrong?
Thanks!
When using constraints, you should do everything with constraints (no setting of frames), so adding one constraint isn't enough to define you positions and sizes. I'm not sure why adding the one constraint negated the sizes you set for the views, but it seems that it does. You should do something like this to fully describe the views,
- (void)viewDidLoad {
[super viewDidLoad];
UIView *viewA = [UIView new];
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewA.backgroundColor = [UIColor redColor];
[self.view addSubview:viewA];
UIView *viewB = [UIView new];
viewB.translatesAutoresizingMaskIntoConstraints = NO;
viewB.backgroundColor = [UIColor grayColor];
[self.view addSubview:viewB];
NSDictionary *dict = NSDictionaryOfVariableBindings(viewA,viewB);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[viewA(==50)]" options:0 metrics:nil views:dict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"[viewB(==50)]" options:0 metrics:nil views:dict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[viewA(==50)][viewB(==50)]" options:NSLayoutFormatAlignAllLeft metrics:nil views:dict]];
}

Resources