How can I change constraint of height with button? - ios

I want to change my calendar height's constraint 124 to zero when calendar button is clicked how can I do that? I want to use button statement but I couldn't at objective c. Thanks.
#property (weak, nonatomic) IBOutlet UIBarButtonItem *calendarButton;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfCalendar;`
- (IBAction)calendarAction:(id)sender {
}

you need to set constant property
heightOfCalendar.constant = requiredheight

You can Do like this to change size on each tap
- (IBAction)calendarAction:(id)sender {
sender.selected = !sender.selected;
heightOfCalendar.constant = (sender.selected)?0:124;
}

Doing it with an animation will looks good
(IBAction)calendarAction:(id)sender {
[UIView animateWithDuration:0.5
animations:^{
heightOfCalendar.constant =0;
[self.view layoutIfNeeded];
}];
}

Write this code in your IBAction:
- (IBAction)calendarAction:(id)sender {
self.heightOfCalendar.constant = 0;
}
Also make sure you have your IBAction connection set properly.
And I see a ` symbol at the end of the following statement:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfCalendar;`

Related

work with two container view using segmented controller

M learn Container View using objective c.
Here, try to display two container one by one using segmented Controller.
Two container use container name is containerViewA, containerViewB.
I am running the app that time only containerViewB is load.
Code:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIView *containerViewA;
#property (weak, nonatomic) IBOutlet UIView *containerViewB;
#end
- (IBAction)ShowComponent:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 0) {
[UIView animateWithDuration:(0.5) animations:^{
self.containerViewA.alpha = 1;
self.containerViewB.alpha = 0;
}];
} else {
[UIView animateWithDuration:(0.5) animations:^{
self.containerViewA.alpha = 0;
self.containerViewB.alpha = 1;
}];
}
}
Update:
Update 1:
Storyboard side panel.
make sure #property (weak, nonatomic) IBOutlet UIView *containerViewA; and #property (weak, nonatomic) IBOutlet UIView *containerViewB; are connected properly.

How to access all labels in my view controller programmatically

I am not sure is this even possible, but I will ask anyway.
This is my ViewController.h
#interface TBL_GameViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *roundText;
#property (strong, nonatomic) IBOutlet UILabel *roundNumber;
#property (strong, nonatomic) IBOutlet UILabel *playerText;
#property (strong, nonatomic) IBOutlet UILabel *playerScore;
#property (strong, nonatomic) IBOutlet UILabel *computerText;
#property (strong, nonatomic) IBOutlet UILabel *computerScore;
#end
And this is one method from .m file
- (void) lablesHiden:(BOOL)on
{
self.roundText.hidden = on;
self.roundNumber.hidden = on;
self.playerText.hidden = on;
self.playerScore.hidden = on;
self.computerText.hidden = on;
self.computerScore.hidden = on;
}
All this is working file.
Question
is the some way to a access all available labels in my view controller programmatically ?
Reason why I am asking this is:
I will have around 10 methods that will need access these labels, to change various properties (color, text, ...).
If tomorrow I add more label, I will also need add new label to all those methods and I would like to avoid that ?
UPDATE
I the end I used this approach
- (NSArray*) getAllLabels
{
NSArray *labels = [[NSArray alloc] initWithObjects:self.roundText, self.roundNumber, self.playerText, self.playerScore, self.computerText, self.computerScore, nil];
return labels;
}
- (void) appear:(BOOL)on
{
for (UILabel *label in [self getAllLabels]) {
label.alpha = 0.0;
}
// more code
}
There absolutely is a way:
for (id label in self.view.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
// do your stuff...
}
}
You get more granular control about which labels to address by using tags. This is also cleaner than doing class introspection.
For example:
#define kPlayer 100
#define kRound 200
#define kComputer 300
#define kText 10
#define kNumber 20
You assign tags e.g. in viewDidLoad like this:
roundText.tag = kRound + kText;
Now there is no need to iterate through all subviews (you just have one iteration per transaction).
for (int x = 100; x < 400; x += 100) {
for (int y = 10; y < 30; y += 10) {
UILabel *label = (UILabel*) [self.view viewWithTag:x+y];
// do something with label
}
}
You can see that you can very conveniently exclude certain labels if you need to.
Also, via KVC, all labels can be accessed like this:
[self.view.subviews filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:#"tag > 99"]];

Change the title of an IBAction from another IBAction

I'm wondering how to call an IBAction from another IBAction.
What I want to do is when I touch a button on previousPage, the title of the nextPage button to change.
In other words I have these IBActions:
- (IBAction)nextPageButtonItemPressed:(UIBarButtonItem *)sender
{
...
}
- (IBAction)previousParageButtonItemPressed:(UIButton *)sender
{
//This is what i want to do
[self nextPageButtonItemPressed:sender.title = #"Next Page"];
}
I have also created an IBOutlet of previousPage.
#property (strong, nonatomic) IBOutlet UIToolbar *nextPageOutlet;
But I can't find the way to do this.
P.S. The nextPage is a button in UIToolbar.
For this, you need to have your UIButton as an IBOutlet property.
So something like:
#property (nonatomic, retain) IBOutlet UIBarButtonItem *previousButton;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *nextButton;
- (IBAction)nextPageButtonItemPressed:(UIBarButtonItem *)sender
{
...
}
- (IBAction)previousPageButtonItemPressed:(UIBarButtonItem *)sender
{
self.nextButton.title = #"Next Page";
}

textFieldShouldBeginEditing didn't response

#import <UIKit/UIKit.h>
#import "HZAreaPickerView.h"
#interface CodeNumberViewController : UIViewController<UITextFieldDelegate, HZAreaPickerDelegate>
#property (weak, nonatomic) IBOutlet UITextField *areaText;
#property (weak, nonatomic) IBOutlet UITextField *cityText;
#property (strong, nonatomic) NSString *areaValue, *cityValue;
#property (strong, nonatomic) HZAreaPickerView *locatePicker;
-(void)cancelLocatePicker;
#end
-----------------below's .m file--------------
#pragma mark - TextField delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField isEqual:self.areaText]) {
[self cancelLocatePicker];
self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self];
[self.locatePicker showInView:self.view];
} else {
[self cancelLocatePicker];
self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCity delegate:self];
[self.locatePicker showInView:self.view];
}
return NO;
}
When I click the UITextField, I found the textFieldShouldBeginEditing even not being called. What's wrong? I already connected the UITextField to the .h file.
I truly hope u can help, if u need more details, I'll add, do not minus my reputation cause I don't have much. Thanks
I guess you didn't set the delegate, in your viewDidLoad set:
self.areaText.delegate = self;
self.cityText.delegate = self;
Did you connect UITextField delegate or mention in your code ??
First you have to add UITexFieldDelegate like you have already done, but you also need to connect the UITextField with the delegate when you initialize it. You can add it programmatically or just connecting it in the xib file(or storyboard).
Programmatically:
areaText; = [[UILabel alloc] initWithFrame:frane];
areaText.delegate = self;
[self.view addSubview:areaText];

resignFirstResponder changes frame value

I have a LoginViewController with two text fields and a button. I used storyboard, so these are not created programmatically. They are below each other.
Here's the .h file
#interface LoginViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *pw1;
#property (weak, nonatomic) IBOutlet UITextField *pw2;
#property (weak, nonatomic) IBOutlet UIButton *loginBtn;
- (IBAction)loginBtnPressed:(id)sender;
- (IBAction)singleTapRecognized:(id)sender;
#end
The pw2 is shown only on the first run, when the user creates a new password and confirms it in pw2. Otherwise it is hidden. In that case I move the button up to put the button closer to pw1.
CGSize size = self.pw2.frame.size;
CGRect rect = self.loginBtn.frame;
CGRect newFrame = CGRectMake(rect.origin.x, rect.origin.y-size.height, rect.size.width,rect.size.height);
[self.loginBtn setFrame:newFrame];
So far, so good. Up to here all works as expected. But now.....
when I leave the pw1 blank, the program checks the field, detects it is empty, hides the keyboard and shows a UIAlertView. This works fine.
when I put in some characters that are not correct, the program does almost the same, but moves the button down to its original position.
same behavior when some characters entered and tapped anywhere in the view
I found out, that this only happens, when I am hiding the keyboard. Otherwise the button stays at its "upped" position as expected.
Here is my keyboard hide:
-(void) hideKeyboard {
if (self.pw1.isFirstResponder)
[self.pw1 resignFirstResponder];
else if (self.pw2.isFirstResponder)
[self.pw2 resignFirstResponder];
}
Any ideas what happens?
Sorry about my english. English is not my native language.
I want to share my solution with you. I am sure there is a more elegant way to code, but this one works fine more me.
I added property in the .h file to hold the new constraint for the button.
#property NSLayoutConstraint *loginButtonVerticalSpace;
Then i add a routine to find the "old" constraint as defined in the storyboard.
-(NSLayoutConstraint *) findButtonConstraintForItem:(id) item
secondItem:(id) secondItem //may be nil
firstAttribute:(NSLayoutAttribute)firstAttribute
secondAttribute:(NSLayoutAttribute)secondAttribute
{
NSArray *cons = self.view.constraints;
for (NSLayoutConstraint *ns in cons) {
if (ns.firstItem==item)
{
if (ns.firstAttribute==firstAttribute)
{
if (secondItem==nil)
return ns;
if (ns.secondItem==secondItem && ns.secondAttribute==secondAttribute)
return ns;
}
}
}
return nil;
}
And at least i changed my "moveButton" method to the following:
- (void) moveButtonUp{
NSLayoutConstraint *consOld = [self findButtonConstraintForItem:self.loginBtn
secondItem:self.pw1
firstAttribute:NSLayoutAttributeTop
secondAttribute:NSLayoutAttributeBottom];
if (!consOld) // should not happen
return;
if (self.loginButtonVerticalSpace) // work already done
return;
CGSize size = self.pw2.frame.size;
self.loginButtonVerticalSpace =[NSLayoutConstraint
constraintWithItem:self.loginBtn
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.pw1
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:consOld.constant-size.height];
self.loginButtonVerticalSpace.priority=1000;
[self.view addConstraint:self.loginButtonVerticalSpace];
[self.view removeConstraint:consOld];
}
Thank you so much, guys....

Resources