Why isn't the camera working on my iPad with MTBBarcodeScanner? - ios

I've written a simple UIViewController that uses MTBBarcodeScanner to scan barcodes. It works perfectly on my iPhone 6s+ running iOS 10, no matter if it's a development build from Xcode or an AdHoc distribution from diawi.com. Unfortunately, it's not working on iPad (also running iOS 10), when distributed through Xcode or the AdHoc thing. My view controller slides in correctly, the buttons work as expected, but the preview window shows no video and no barcodes ever get read.
Before this view controller loads, I've already checked for permission with the user. I got the popup from iOS and granted it on both platforms. The Settings app for the iPad shows that I indeed have access to the camera.
Here's my code.
#import "BarCodeScannerViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <MTBBarcodeScanner/MTBBarcodeScanner.h>
#interface BarCodeScannerViewController ()
#property (nonatomic, strong) MTBBarcodeScanner *scanner;
#end
#implementation BarCodeScannerViewController
- (void) loadView; {
[super loadView];
[self setTitle:#"Scan Bar Code"];
UIView *preview = [[UIView alloc] init];
[[self view] addSubview:preview];
[preview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setScanner:[[MTBBarcodeScanner alloc] initWithMetadataObjectTypes:#[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeQRCode] previewView:preview]];
UIView *buttonBar = [[UIView alloc] init];
[[self view] addSubview:buttonBar];
[buttonBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[buttonBar setBackgroundColor:[UIColor colorWithWhite:0 alpha:.5]];
UIButton *cancelButton = [[UIButton alloc] init];
[buttonBar addSubview:cancelButton];
[cancelButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[cancelButton addTarget:self action:#selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
UIButton *torchToggle = nil;
if ([[self scanner] hasTorch]) {
UIButton *torchToggle = [[UIButton alloc] init];
[buttonBar addSubview:torchToggle];
[torchToggle setTranslatesAutoresizingMaskIntoConstraints:NO];
[torchToggle addTarget:[self scanner] action:#selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
[torchToggle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[torchToggle setTitle:#"Toggle Light" forState:UIControlStateNormal];
}
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:44.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5.0]];
if ([[self scanner] hasTorch]) {
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:torchToggle attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeRight multiplier:1.0 constant:-5.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:torchToggle attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5.0]];
}
[_scanner startScanningWithResultBlock:^(NSArray *codes) {
for (AVMetadataMachineReadableCodeObject *code in codes) {
if ([code stringValue]) {
[_scanner stopScanning];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
if ([self barCodeScanned]) [self barCodeScanned]([code stringValue], self);
break;
}
}
} error:nil];
}
- (void) cancel;
{
[[self scanner] stopScanning];
[self dismissViewControllerAnimated:YES completion:nil];
}
#end

Thanks to Mike Buss for assisting me with this. He told me that I was probably starting the camera too early. He suggested I move the startScanningWithResultBlock: call to viewDidAppear:animated. I tested it with my iPad and it's working great now.

Related

Horizontally Placed Equal Width Equal Horizontal Space Buttons With Auto Layout

I have written the following code to achieve
Horizontally spaced equal width fixed height equal horizontal spaced three button but somehow it's not working fine. Can anyone rectify this ?
UIButton *cancelBtn = [UIButton new];
cancelBtn.frame = CGRectMake(10, 5, (popupView.bounds.size.width-40)/3, 30);
[cancelBtn setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelBtn setBackgroundColor:[UIColor lightGrayColor]];
[popupView addSubview:cancelBtn];
UIButton *resetBtn = [UIButton new];
resetBtn.frame = CGRectMake(cancelBtn.frame.origin.x+cancelBtn.bounds.size.width+10, 5, (popupView.bounds.size.width-40)/3, 30);
[resetBtn setTitle:#"Reset" forState:UIControlStateNormal];
[resetBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[resetBtn setBackgroundColor:[UIColor darkGrayColor]];
[popupView addSubview:resetBtn];
UIButton *doneBtn = [UIButton new];
doneBtn.frame = CGRectMake(popupView.bounds.size.width-10-((popupView.bounds.size.width-40)/3), 5, (popupView.bounds.size.width-40)/3, 30);
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn setBackgroundColor:[UIColor blackColor]];
[popupView addSubview:doneBtn];
cancelBtn.translatesAutoresizingMaskIntoConstraints = NO;
[cancelBtn addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f]];
resetBtn.translatesAutoresizingMaskIntoConstraints = NO;
[resetBtn addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10.0f]];
doneBtn.translatesAutoresizingMaskIntoConstraints = NO;
[doneBtn addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10.0f]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:doneBtn attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
How to fix this ?
I want to achieve a popup like this.
enter image description here
use stackView instead of UIView there are lots of flexibility.
You don't have to think about child view's autolayout inside stackView.
Stack View has a bunch of features to satisfy your needs
Code:
-(void)createStackViewWithButton {
UIButton *doneBtn = [UIButton new];
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn setBackgroundColor:[UIColor blackColor]];
[doneBtn.heightAnchor constraintEqualToConstant:40].active = true;
[doneBtn.widthAnchor constraintEqualToConstant:100].active = true;
UIButton *doneBtn2 = [UIButton new];
[doneBtn2 setTitle:#"Cancel" forState:UIControlStateNormal];
[doneBtn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[doneBtn2 setBackgroundColor:[UIColor greenColor]];
[doneBtn2.heightAnchor constraintEqualToConstant:40].active = true;
[doneBtn2.widthAnchor constraintEqualToConstant:100].active = true;
UIButton *doneBtn3 = [UIButton new];
[doneBtn3 setTitle:#"Edit" forState:UIControlStateNormal];
[doneBtn3 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn3 setBackgroundColor:[UIColor blueColor]];
[doneBtn3.heightAnchor constraintEqualToConstant:40].active = true;
[doneBtn3.widthAnchor constraintEqualToConstant:100].active = true;
UIStackView *stackView = [[UIStackView alloc] init];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionEqualSpacing;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 0;
[stackView addArrangedSubview:doneBtn];
[stackView addArrangedSubview:doneBtn2];
[stackView addArrangedSubview:doneBtn3];
stackView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:stackView];
//Layout for Stack View
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = true;
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = true;
}
output:
Image 1
Image 2
They are appearing one over another.
SOLVED ! Fixed it.
UIButton *cancelBtn = [UIButton new];
cancelBtn.frame = CGRectMake(10, 5, (popupView.bounds.size.width-40)/3, 30);
[cancelBtn setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelBtn setBackgroundColor:[UIColor lightGrayColor]];
[popupView addSubview:cancelBtn];
UIButton *resetBtn = [UIButton new];
resetBtn.frame = CGRectMake(cancelBtn.frame.origin.x+cancelBtn.bounds.size.width+10, 5, (popupView.bounds.size.width-40)/3, 30);
[resetBtn setTitle:#"Reset" forState:UIControlStateNormal];
[resetBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[resetBtn setBackgroundColor:[UIColor darkGrayColor]];
[popupView addSubview:resetBtn];
UIButton *doneBtn = [UIButton new];
doneBtn.frame = CGRectMake(resetBtn.frame.origin.x+resetBtn.bounds.size.width+10, 5, (popupView.bounds.size.width-40)/3, 30);
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[doneBtn setBackgroundColor:[UIColor blackColor]];
[popupView addSubview:doneBtn];
cancelBtn.translatesAutoresizingMaskIntoConstraints = NO;
[cancelBtn addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]];
resetBtn.translatesAutoresizingMaskIntoConstraints = NO;
[resetBtn addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cancelBtn attribute:NSLayoutAttributeRight multiplier:1.0f constant:10.0f]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:doneBtn attribute:NSLayoutAttributeLeft multiplier:1.0f constant:-10.0f]];
doneBtn.translatesAutoresizingMaskIntoConstraints = NO;
[doneBtn addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:30]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTop multiplier:1.0f constant:5.0f] ];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:doneBtn attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:popupView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]];
// +++++++ Equal width constraints +++++++
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:cancelBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:resetBtn attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0]];
[popupView addConstraint:[NSLayoutConstraint constraintWithItem:resetBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:doneBtn attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0]];

Even after giving vertical contraints the individual views are overlapping

I am actually trying to have UIView which houses UIImageView and UILabel (side by side). There are multiple such pairs present. I need these pairs one below the other. But after the second pair, the overlapping starts in spite of giving vertical contraints. The code below is actually resulting in Views getting overlapped (third and fourth getting on to second) I am unable to know exactly what is going wrong. Appreciate if somebody could point out this
UIView *bottomCaseStudiesLyt = [[UIView alloc]init];
bottomCaseStudiesLyt.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:bottomCaseStudiesLyt];
NSLayoutConstraint* bottomCaseStudiesleftConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[contentView addConstraint:bottomCaseStudiesleftConstraint];
NSLayoutConstraint* bottomCaseStudiesTopConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:details3View attribute:NSLayoutAttributeBottom multiplier:1.0f constant:10.0f];
[contentView addConstraint:bottomCaseStudiesTopConstraint];
NSLayoutConstraint* bottomCaseStudiesRightConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[contentView addConstraint:bottomCaseStudiesRightConstraint];
NSLayoutConstraint* bottomCaseStudiesBottomConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[contentView addConstraint:bottomCaseStudiesBottomConstraint];
//NSArray *bottomCaseStudiesVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[headingDetailsView]-[details2View]-[details3View]-[bottomCaseStudiesLyt]|" options:0 metrics:nil views:#{#"headingDetailsView": headingDetailsView,#"details2View": details2View,#"details3View": details3View,#"bottomCaseStudiesLyt": bottomCaseStudiesLyt}];
//[contentView addConstraints:bottomCaseStudiesVConstraints];
UIView *firstView = [[UIView alloc]init];
[firstView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: firstView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:nil whichimage:#"ic_action_easy" whattext:#"Rediculously easy. Takes less than 30 seconds to build a room and go live" mynewview:firstView];
UIView *secondView = [[UIView alloc]init];
[secondView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: secondView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:firstView whichimage:#"ic_action_amaze" whattext:#"Engage members with great content, services, offers, polls, notification, quiz and more" mynewview:secondView];
UIView *thirdView = [[UIView alloc]init];
[thirdView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: thirdView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:secondView whichimage:#"ic_action_subscribers" whattext:#"Members ? No limit! There is a room for all. Go ahead and promote your room." mynewview:thirdView];
UIView *fourthView = [[UIView alloc]init];
[fourthView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: fourthView];
[self addImageAndDetails:bottomCaseStudiesLyt previousview:thirdView whichimage:#"ic_action_crossplatform" whattext:#"Your room can be accessed from any platform or device." mynewview:fourthView];
NSArray *bottomViewVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[firsView]-[secondView]-[thirdView]-[fourthView]|" options:0 metrics:nil views:#{#"firsView": firstView,#"secondView": secondView,#"thirdView": thirdView,#"fourthView": fourthView}];
[bottomCaseStudiesLyt addConstraints:bottomViewVConstraints];
// GetStarted button
self->m_ObjGetStartedBut = [[UIButton alloc]init];
[self->m_ObjGetStartedBut setTitle: [NSString stringWithFormat:#"Get Started"] forState:UIControlStateNormal];
self->m_ObjGetStartedBut.backgroundColor = [UIColor redColor];
[self->m_ObjGetStartedBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self->m_ObjGetStartedBut.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjGetStartedBut.layer.cornerRadius = 10;
self->m_ObjGetStartedBut.clipsToBounds = YES;
[parentView addSubview:self->m_ObjGetStartedBut];
NSDictionary *myTopViews = #{
#"scrollView": self->myScrollView,
#"submitButton": self->m_ObjGetStartedBut
};
NSArray *myTopVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[scrollView]-[submitButton(40)]|" options:0 metrics:nil views:myTopViews];
NSArray *myTopHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[submitButton]-|" options:0 metrics:nil views:#{#"submitButton": self->m_ObjGetStartedBut}];
[parentView addConstraints:myTopVConstraints];
[parentView addConstraints:myTopHConstraints];
//[self->m_ObjGetStartedBut addTarget:self action:#selector(buttonIsReleased:) forControlEvents: UIControlEventTouchUpInside];
//[self->m_ObjGetStartedBut setTag:1];
}
-(UIView *)addImageAndDetails:(UIView *)localparentView previousview:(UIView *)prevView whichimage:(NSString *)imageName whattext:(NSString *)relatedText mynewview:(UIView *)itemView
{
NSLayoutConstraint* topViewleftConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[localparentView addConstraint:topViewleftConstraint];
NSLayoutConstraint* topViewRightConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[localparentView addConstraint:topViewRightConstraint];
NSLayoutConstraint* topViewTopConstraint = nil;
if(prevView == nil)
{
topViewTopConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[localparentView addConstraint:topViewTopConstraint];
}
else
{
//topViewTopConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:prevView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
//[localparentView addConstraint:topViewTopConstraint];
}
UIImage *easyToCreateImg = [UIImage imageNamed:imageName];
UIImageView *easyToCreateImgView = [[UIImageView alloc] initWithImage:easyToCreateImg];
easyToCreateImgView.contentMode = UIViewContentModeScaleToFill;
easyToCreateImgView.translatesAutoresizingMaskIntoConstraints = NO;
easyToCreateImgView.clipsToBounds = YES;
[itemView addSubview:easyToCreateImgView];
NSLayoutConstraint* easyToCreateImgLeftConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateImgLeftConstraint];
NSLayoutConstraint* easyToCreateImgTopConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeTop multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateImgTopConstraint];
NSLayoutConstraint *easyToCreateImgHtConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80.0f];
[itemView addConstraint:easyToCreateImgHtConstraint];
NSLayoutConstraint *easyToCreateImgWidConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80.0f];
[itemView addConstraint:easyToCreateImgWidConstraint];
UILabel* easyToCreateLblView = [[UILabel alloc]init];
[easyToCreateLblView setText:relatedText];
easyToCreateLblView.numberOfLines = 0;
UIFont *newHeadingViewLblFont = [UIFont fontWithName:#"Arial" size:13];
[easyToCreateLblView setFont:newHeadingViewLblFont];
[easyToCreateLblView setTextColor:[UIColor blackColor]];
easyToCreateLblView.translatesAutoresizingMaskIntoConstraints = NO;
[itemView addSubview:easyToCreateLblView];
NSLayoutConstraint* easyToCreateLblTopConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeCenterYWithinMargins relatedBy:NSLayoutRelationEqual toItem:easyToCreateImgView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateLblTopConstraint];
NSLayoutConstraint* easyToCreateLblLeftConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:easyToCreateImgView attribute:NSLayoutAttributeRight multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateLblLeftConstraint];
NSLayoutConstraint* easyToCreateLblRightConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[itemView addConstraint:easyToCreateLblRightConstraint];
return itemView;
}
I am not sure if my interpretation is right or wrong. What I observed is that when we give width and height constraints to the first view, this I think is not respected by the subsequent views that have been added. However, it does seem to respect the other constraints (top, bottom, leading, trailing). So when I started giving the same magnitude of width and height to the other subsequent views, then the arrangement came out as expected. Thoughts from the community are welcome.

All images of UIScrollView are getting placed in the beginning

I am new to iOS programming. I want to generate all the controls using coding and then apply constraints to achieve autosize feature. I had achieved almost my requirement except for one problem and that is all images of my UIScrollView are getting placed at very beginning and rest of the UIScrollView stays empty. I think I am having some sort of problem with my constraints and currently I am not able to resolve it.
This is my code
self.bgView.image = [UIImage imageNamed:#"bg.png"];
NSDictionary *viewDictionary = #{#"bgImage":self.bgView,#"scrollView":self.scrollView};
NSDictionary *position = #{#"vSpacing":#0,#"hSpacing":#0};
//here I had specified the size of the background image corresponding to the view
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|-hSpacing-[bgImage]-hSpacing-|" options:0 metrics:position views:viewDictionary];
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-vSpacing-[bgImage]-vSpacing-|" options:0 metrics:position views:viewDictionary];
[self.view addConstraints:constraint_POS_H];
[self.view addConstraints:constraint_POS_V];
//here I am specifying the size of scroll view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.bgView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.scrollView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.bgView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
//self.view.autoresizesSubviews = YES;
self.scrollView.pagingEnabled = YES;
NSInteger numberOfViews = photoArray.count;
for (int i=0; i < numberOfViews; i++) {
CGFloat myOrigin = i * self.view.frame.size.width;
NSLog(#"self.view.frame.size.width : %f",self.view.frame.size.width);
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.scrollView.frame.size.height)];
[myView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:myView];
//here I am specifying the size of uiview
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.scrollView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0]];
//here I am specifying the position of uiview
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:myView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
UIImageView *photos = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, myView.frame.size.width, self.scrollView.frame.size.height)];
//self.photos = [UIImageView new];
[photos setTranslatesAutoresizingMaskIntoConstraints:NO];
photos.image = [photoArray objectAtIndex:i];
[myView addSubview:photos];
//here I am specifying the size of image view within scroll view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:myView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0]];
//here I am specifying the position of the image view
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:photos
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:myView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
self.scrollView.delegate = self;
[self.scrollView addSubview:myView];
NSLog(#"self.myView.frame.size.width : %f",myView.frame.size.width);
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
self.scrollView.frame.size.height);
CGPoint scrollPoint = CGPointMake(0, 0);
[self.scrollView setContentOffset:scrollPoint animated:YES];
[self.view addSubview:self.scrollView];
you can easily solve it by just using reset to suggested constraints in storyboard.First select viewController and then press right bottom menu and select reset to suggested constraints in All views tab
This worked for me.

Centre the button in view use constraints programmatically in iOS 8

I have a subview and added 1 button. I want to add this button in the center of that view. I use Autolayout so i need to set the constraints for this button programmatically.
I tried this code but the play button is not in the center.
[self.moviePlayer.view addSubview:self.playbtn];
self.playbtn.translatesAutoresizingMaskIntoConstraints = NO;
[self.moviePlayer.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playbtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual
toItem:self.moviePlayer.view attribute:NSLayoutAttributeCenterX multiplier:0.667 constant:0]];
Please help me to correct it. Thanks in advance.
You can use the NSLayoutAttributeCenterX and NSLayoutAttributeCenterY attributes to center your play button like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initViews];
[self initConstraints];
}
-(void)initViews
{
self.picture = [[UIImageView alloc] init];
self.picture.image = [UIImage imageNamed:#"bg"];
self.playButton = [[UIButton alloc] init];
[self.playButton setImage:[UIImage imageNamed:#"playButton"] forState:UIControlStateNormal];
[self.view addSubview:self.picture];
[self.view addSubview:self.playButton];
}
-(void)initConstraints
{
self.picture.translatesAutoresizingMaskIntoConstraints = NO;
self.playButton.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"picture": self.picture,
#"playButton": self.playButton
};
// picture constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[picture]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[picture]|" options:0 metrics:nil views:views]];
// play button constraints
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
You get something like this:
You can use this code to center any item (es. self.btn) in its superview:
[self.btn.superview addConstraint:[NSLayoutConstraint
constraintWithItem:self.btn.superview
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.btn
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.btn.superview addConstraint:[NSLayoutConstraint
constraintWithItem:self.btn.superview
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.btn
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
I also wrote an article about aligning any UIView objects without using Storyboards on my blog.

Can't remove/hide recently added view from self.navigationcontroller.view

I have a Navigationviewcontroller with a tableview in it. I am adding a UIView to self.navigationcontroller.view and constraints for Auto Layout. When I try to remove it from superview or to hide it, nothing happens.
Any ideas?
How I create the View and Constraints
-(void)doneWithTraining{
//Create Backgroundview
UIView *groupView = [[UIView alloc] init];
[groupView setTranslatesAutoresizingMaskIntoConstraints:NO];
groupView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
groupView.tag = 999;
groupView.alpha = 0.0;
[self.navigationController.view addSubview:groupView];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0]];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0f]];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
[self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.view
attribute:NSLayoutAttributeHeight
}
multiplier:1.0
constant:0.0]];
//Animate View
[UIView animateWithDuration:0.2 animations:^(void) {
groupView.alpha = 1.0;
}];
How I try to remove the View
- (void)closeFinishAlert{
UIView *grouView = [self.navigationController.view viewWithTag:999];
grouView.hidden = YES;
NSLog(#"Check if closeFinishAlert is called");
//[self.navigationController popViewControllerAnimated:YES];
}
What I also tried
This removes the view an its content, but after this there is no user interaction possible. It looks like a crash, but Xcode tells me that the app is still running.
- (void)closeFinishAlert{
UIView *groupView = [self.navigationController.view viewWithTag:999];
for (UIView *subview in groupView.subviews) {
subview.hidden = YES;
[subview removeFromSuperview];
}
[_groupView removeFromSuperview];
//[self.navigationController popViewControllerAnimated:YES];
}
If you are actually just presenting a view which you will remove for sure later on,
then its better to create a ViewController class for your view and present it modally using :
[self presentViewController:yourViewControllerObject animated:NO completion:nil];
you can later on remove this view using:
[self dismissViewControllerAnimated:NO completion:nil];
hope this helps !
Thanks to "GoGreen" green I found a solution which works for me.
Create Viewcontroller from ViewDidAppear
- (void)viewDidAppear:(BOOL)animated{
if (executedExercises.count == [_fetchedResultsController.fetchedObjects count] && self.groupView == FALSE) {
[self performSelector:#selector(doneWithTraining) withObject:nil afterDelay:0.0];
}
}
Header file
#property (strong, nonatomic) UIViewController *groupView;
Creating the Viewcrontroller and its content
-(void)doneWithTraining{
_groupView = [[UIViewController alloc] init];
[self presentViewController:_groupView animated:YES completion:nil];
_groupView.view.backgroundColor = [UIColor whiteColor];
//SubTitle
UILabel *subTitleLabel = [[UILabel alloc] init];
[subTitleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:18.0]];
subTitleLabel.textColor = [BackgroundLayer gray];
[subTitleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
subTitleLabel.numberOfLines = 5;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.textAlignment = NSTextAlignmentCenter;
[_groupView.view addSubview:subTitleLabel];
subTitleLabel.text = subTitleText;
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationLessThanOrEqual
toItem:_groupView.view
attribute:NSLayoutAttributeWidth
multiplier:0.0
constant:300.0f]];
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:_groupView.view
attribute:NSLayoutAttributeHeight
multiplier:0.0
constant:140]];
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:_groupView.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_groupView.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];}
Removing ViewController
- (void)closeFinishAlert{
[_groupView dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}

Resources