Best way to update constraint outlet constant programmatically - ios

I have a width constraint in a .xib file which I connected to an outlet, and I want to change its constant for iPhone 6.
Between this one:
//
// MyViewController.m
//
#import "MyViewController.h"
#interface MyViewController ()
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *constraintViewWidth;
#property (nonatomic) BOOL isConstraintsUpdated;
#end
#implementation MyViewController
- (void)updateViewConstraints {
if (!self.isConstraintsUpdated) {
if (CGRectGetHeight([UIScreen mainScreen].bounds) == 667) {
constraintViewWidth.constant = 60;
}
self.isConstraintsUpdated = YES;
}
[super updateViewConstraints];
}
#end
and this one:
//
// MyViewController.m
//
#import "MyViewController.h"
#interface MyViewController ()
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *constraintViewWidth;
#property (nonatomic) BOOL isLayoutLoaded;
#end
#implementation MyViewController
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!self.isLayoutLoaded) {
if (CGRectGetHeight([UIScreen mainScreen].bounds) == 667) {
constraintViewWidth.constant = 60;
}
self.isLayoutLoaded = YES;
}
}
#end
which is the preferred one?
I tried using the first, but it doesn't work, as the custom constant gets overridden by the one in the .xib file, though it would make the most sense to change it in updateViewConstraints.
The second one works but I don't see why would I want to change a constraint when the layout finishes.

Related

Linking two UITextView to scroll together if one is scrolled

I have two text views beside each other with multiple lines of text.
How can I make them both scroll together based on scrolling either one of them at the same time?
First
#interface ViewController : UIViewController <UITextViewDelegate>
second make both textViews delagates
tv1.delegate = self
tv2.delegate = self
Implement
-(void)scrollViewDidScroll:(UIScrollView *)inScrollView {
self.tv1.contentOffset = inScrollView.contentOffset;
self.tv2.contentOffset = inScrollView.contentOffset;
}
Here is a demo txView
Edit
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextView *tv1;
#property (weak, nonatomic) IBOutlet UITextView *tv2;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tv1.delegate = self;
self.tv2.delegate = self;
}
- (void)scrollViewDidScroll:(UIScrollView *)inScrollView {
self.tv1.contentOffset = inScrollView.contentOffset;
self.tv2.contentOffset = inScrollView.contentOffset;
}

custom delegate method not call

i have first display start_game screen than after when i click button that time display popupview using xib.in xib class i have create delegate method.when i close the popupview that time call delegate method but not calling
here is my delegate class
.h File
#import
#protocol digbuttonalertdelegate;
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
.m File
#import "digbuttonalert.h"
#import "suggestion_alert.h"
#import "UIViewController+CWPopup.h"
#import "zoom_alert.h"
#interface digbuttonalert ()
{
bool status;
}
#end
#implementation digbuttonalert
- (void)viewDidLoad {
[super viewDidLoad];
status=0;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[self.bg_image setUserInteractionEnabled:YES];
[self.bg_image addGestureRecognizer:singleTap];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)close:(id)sender {
}
-(void)tapDetected{
NSLog(#"single Tap on imageview");
if(status==0)
{
self.lbl_title.text=#"As you walk you will discover the hidden map.This circle will show your progress.";
status=1;
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
if ([self.delegate respondsToSelector:#selector(digalertclose)]) {
[self.delegate digalertclose];
}
}
}
here this class i want to call method
#import "digbuttonalert.h"
#interface start_games () <MJSecondPopupDelegate,digbuttonalertdelegate>
{
- (void)viewDidLoad {
digbuttonalert *next=[[digbuttonalert alloc]init];
next.delegate=self;
next.modalTransitionStyle=UIWebPaginationModeRightToLeft;
next.modalPresentationStyle=17;
[self presentViewController:next animated:YES completion:nil];
}
- (void)digalertclose
{
[self StartTimer];
[[NSUserDefaults standardUserDefaults]setObject:#"false" forKey:#"alertstatus"];
}
Change .m file with below :
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
And follow basic tutorial steps : enter link description here
Please implement digalertclose delegate in start_games controller like this
-(void)digalertclose {
}
You make digalertclose optional thats why crash does not occur if you remove optional keyword from digbuttonalertdelegate protocol you can see that there will be a crash because you try to fire a delegate but does not implement it.
In your digbuttonalert.h protocol is defined twice.
There is no #end for the interface.
Should be something like
ClassA.h
#protocol YourProtocol <NSObject>
#optional
-(void)yourProtocolOptionalMethod
#end
#interface ClassA.h
#property (weak, nonatomic) id <YourProtocol> delegate;
#end
ClassA.m
-(void) someClassAMethod {
[self.delegate yourProtocolOptionalMethod];
}
ClassB.h
#import "ClassA.h"
#interface ClassB <YourProtocol>
#end
ClassB.m
-(void) someClassBMethod {
ClassA *classA = [ClassA alloc] init];
classA.delegate = self;
}
After setting the delegate now when you will call the delegate from ClassA then it will trigger the protocol implemented method in ClassB
-(void)yourProtocolOptionalMethod {
NSlog(#"");
}
On your class , you need to declare the delegate like this:
#interface YourViewController () < digbuttonalertdelegate >

How to make the keyboard disappear programmatically in iOS

I am having trouble getting the keyboard to disappear after entering text. I have many solutions for previous versions of Xcode, but nothing for Xcode 7. My current ViewController.h file looks like:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *txtUsername;
#property (strong, nonatomic) IBOutlet UITextField *txtPassword;
#end
My .m file looks like:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize txtUsername;
#synthesize txtPassword;
- (void)viewDidLoad {
[super viewDidLoad];
self.txtUsername.delegate = self;
self.txtPassword.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
#end
I have assigned the delegate from the textField to the ViewController as well.
Update The View I was working with was not assigned to the ViewController class and therefore did not inherit the textFielfShouldReturn method I needed.
Try:
[self.view endEditing:YES];

How to change variable of custom UIView from ViewController

I have created a custom UIView (ProgressView) where I draw a shape imported via StyleKit from PaintCode.
Below are the codes. I have declared instance variable property in my custom UIView and when I try to change property from ViewController, It does not work.
ProgressView.h
#import <UIKit/UIKit.h>
#interface ProogressView : UIView
#property (nonatomic) float daysFraction;
#property (nonatomic) float pagesFraction;
#end
ProgressView.m
#import "ProgressView.h"
#import "StyleKitName.h"
#import "ViewController.h"
#implementation ProgressView
#synthesize daysFraction = _daysFraction;
#synthesize pagesFraction = _pagesFraction;
- (void)drawRect:(CGRect)rect {
// Drawing code
[StyleKitName drawCanvas1WithDaysFraction:self.daysFraction pageFraction:self.pagesFraction];
}
-(void)awakeFromNib {
[super awakeFromNib];
self.pagesFraction = 0;
self.daysFraction = 0;
}
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
*ViewController.m**
#import "ViewController.h"
#import "ButtonAnimation.h"
#import "ProgressView.h"
#import "StyleKitName.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet ButtonAnimation *buttonView;
#property (weak, nonatomic) IBOutlet UIButton *actionButton;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ProgressView *new =[[ ProgressView alloc]init];
new.daysFraction = 0.7f; // here I am setting value to variable in custom view ProgressView but not working.
}
- (IBAction)animateTheButton:(id)sender {
self.buttonView.layer.backgroundColor = [UIColor clearColor].CGColor;
[self.buttonView addErrorAnimation];
}
#end
You need to add this view to UIViewController's view:
[self.view addSubview:progressView];
Later, you must also set a frame. Eg
[progressView setFrame:self.view.bounds];
You may do it in viewDid/WillLayoutSubviews method to change it on rotation / window resize event.
BTW, do not name your view as new, it's horrible. Such name doesn't even tell what kind of variable is it.

How do you hook up a custom UIView?

Writing my first iphone app through this tutorial--I have a custom UIView with the method showDie and I have two UIView elements on my storyboard that I've hooked up to my ViewController. However, both the UIView elements have the error message "No visible #interface for 'UIView' declares the selector 'showDie'. I think that's because the UIViews aren't subclassing XYZDieView, but when I control-clicked from my Storyboard, only UIView appeared in the dropdown--no XYZDieView. I tried just changing the text manually in the ViewController.h, but that didn't work (I didn't think it would). Am I on the right track? How do I use my subclass? (xcode 5)
XYZDieView.m
-(void)showDie:(int)num
{
if (self.dieImage == nil){
self.dieImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
[self addSubview:self.dieImage];
}
NSString *fileName = [NSString stringWithFormat:#"dice%d.png", num];
self.dieImage.image = [UIImage imageNamed:fileName];
}
XYZViewController.h
#import <UIKit/UIKit.h>
#import "XYZDieView.h"
#interface XYZViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *sumLabel;
#property (strong, nonatomic) IBOutlet UIButton *rollButton;
#property (strong, nonatomic) IBOutlet UIView *leftDieView;
#property (strong, nonatomic) IBOutlet UIView *rightDieView;
#end
XYZViewController.m
#import "XYZViewController.h"
#import "XYZDiceDataController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)rollButtonClicked:(UIButton *)sender {
XYZDiceDataController *diceDataController = [[XYZDiceDataController alloc] init];
int roll = [diceDataController getDiceRoll];
int roll2 = [diceDataController getDiceRoll];
[self.leftDieView showDie:roll];
[self.rightDieView showDie:roll2];
int sum = roll + roll2;
NSString *sumText = [NSString stringWithFormat:#"Sum is %i", sum];
self.sumLabel.text = sumText;
}
#end
Yes you're right, the problem is subclassing. In order to have a view conform to a subclass in the interface builder you need to
Click on the UIView in your storyboard
Open the right inspector panel
Click on the third tab for identity inspector
Add your class
Then try to connect it to your ViewController again.
Edit: Afterwards you may need to cast XYZDieView myView = (XYZDieView*)leftDieView;

Resources