Update Label in scrollview with pageIndex - ios

I have a scroll view in which i have a label. i have to update the label's text each time the user scrolls the scrollview. i want to update the label's value with pageindex.I am getting the page index in scrollviewdelegate method but unable to update it in label as the label gets loaded in viewDidload. Any way to refresh or update the label's text from scrollview delegate method?

HERE pageNumber is number which update and get when you scroll UIScrollView
Write this code in where your get pageNumber
Yourlbl.text = pageNumber;

Make the label a property and use the property to set the labels text.
Property (assuming arc enabled project):
#property (nonatomic, strong) UILabel *myLabel;
ViewDidLoad:
- (void)viewDidLoad {
self.myLabel = [UILabel new];
}
and update your label texts from inside the viewController like this:
self.myLabel.text = #"test";

take all the values in one array and assign the label text based on the pageindex like below
label.text=[titlesArray objectAtIndex: pageindex];
for achieving this one you can take label globally.
EDIT:-
for achieving this one you can do two ways.
1.take UILabel in Xib and connect the label with some name and you can set the labelname.text in scrollviewDidEnd method .
2.take uilabel programatically and define that one in .h file and change the textscrollviewDidEnd particular method.

Related

How to set hide and show view using constraints like auto layout /StackView

I have a larger screen to design, it had plus button, for adding more fields in it. For example, i need to add 2 Email/Phone number, when i press plus button it will show one more textfield with in Email header as vice versa. If i use hide and show method programmatically, the rest of the space will be blank. For this i need to set constraint and setLayout for view. How is it possible?
I would like to give idea so quick look at it.
Take a scroll view inside take one view that having all field your textfield like email and more text that you want.
Now design all field whatever you have to show.
add
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *ViewHeightConstraints;
for container view when you hide textfield just subtract height of all hideTextfield to ViewHeightConstraints.
ok when you hide textfield do something like this:
ViewHeightConstraints.constant = 0;
_addressView.hidden = YES;
[self.view layoutIfNeeded];
ok when you Show textfield do something like this:
ViewHeightConstraints.constant = 243;
_addressView.hidden = NO;
[self.view layoutIfNeeded];
whenever you change constraints write this Everytime.
[self.view layoutIfNeeded];
for setting frame for your main View Set height constraints for MainView. i have done like this.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *containerHeightConstraints;
_containerHeightConstraints.constant = _bottomView.frame.origin.y+_bottomViewHeight.constant;
[self.view layoutIfNeeded];
Hope it will work.
You need to take a IBOutlet of the field height constraint for this.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *fieldConstraintHeight;
Now when you want to hide it use
fieldConstraintHeight.constant = 0;

Resize UILabel based on content inside UITableViewCell iOS

I am using a UITableViewCell which contains few ImageView and Labels. I have given the image of how the cell looks for your reference. I need the Content label to expand and contract based on the text within it, without disturbing any other views inside the cell. I am new to AutoLayouts and I am facing issues with it. Please help.
You need to add a NSLayoutConstraint in the storyboard and then hook it up to a property in your code. Here is an example of one I did with width, but you can do the same with height.
The referencing outlet in the .h file is:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintAuctionHouseNameWidth;
Then in the .m Controller file I figure out the size of the new label and set the constraint to that as shown below.
You then need to figure out the size of the text in your label, which is going to determine the size of the label to show it in. Here is how I do it: (auctionHouseObject.name is the text that goes into the label)
//calculate width of label
CGRect r = [auctionHouseObject.name boundingRectWithSize:CGSizeMake(350, 0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont fontWithName:#"Montserrat-Bold" size:15]}
context:nil];
Then I set the width programmatically. (auctionHouseNameLabelMaxWidth is different depending on the screen width of the phone)
if(r.size.width < auctionHouseNameLabelMaxWidth){
cell.constraintAuctionHouseNameWidth.constant = r.size.width + 2;
}
else{
cell.constraintAuctionHouseNameWidth.constant = auctionHouseNameLabelMaxWidth;
}
Set the other constraints in the storyboard and Autolayout should take care of the rest.

Allocating UILabel in UICollectionViewCell

I making app with collection view and different layout for portrait and landscape orientation and i am allocating UIlabel for every cell.
This is how i do it.
UILabel *scheduleCellLabel = [[UILabel alloc] init];
scheduleCellLabel.textAlignment = NSTextAlignmentCenter;
scheduleCellLabel.frame = CGRectMake(0, 0,scheduleCell.bounds.size.width, scheduleCell.bounds.size.height);
[scheduleCell addSubview:scheduleCellLabel];
Problem is that i reload collection view everytime the orientation changes, but the old cells stays in collection view. I tried to remove the label from cell on load, but that is not the issue.
This is how it looks:
Thanks in advance.
In my opinion, it's not good practice to put subview-adding code in cellForRowAtIndexPath unless you need to; for instance, if some cells have a subview, and some do not based on the indexPath. You should either make the cell in IB, or subclass the cell, and add any subviews in the cell's init method. That way, you don't have to remove views before you re-add them, or check if they exist before you add one; that just complicates your cellForRowAtIndexPath method.
Delete and add the label in cellForItemAtIndexPath. First ull check if label is in the cell, if yes, remove it from superview, then create new label and add it to its contentview.
Probably, you allocate your uilabel in cellForRowAtIndexPath method and it appears again and again when you reload your collectionView.
To solve your problem,just define your label as a property and allocate it in the viewDidLoad method and do what you want in cellForRowAtIndexPath method. Hope this will help.
For example, this is your header file:
#property (retain,nonatomic) IBOutlet UILabel *label;
and this is the viewDidLoad method of implemantation file:
_label = [[UILabel alloc] init];

iOS Centring UITableViewCell label with disclosure indicator

I am looking for a way to enter a label within a table cell that also has a disclosure indicator. The problem i'm having at the moment is that it seems like the disclosure indicator is being ignored when calculating the label's positions
Heres a picture:
So as you can see the label is centred in the area between the left side of the cell and the left side of the indicator, if it was centred in the cell it would sit below the nav bar heading.
Any help is appreciated thankyou
From within the storyboard
Okay, first an explanation for your issue. It has to do with the anatomy of a UITableViewCell. With anatomy, I mean the fact that the UITableViewCell for you is just a container for another container, which is the contentView (you can also see this one in your storyboard).
When you are operating in Storyboards, you are solely operating on the contentView, not on the actual UITableViewCell. So, when you setup your UILabel to be centered on the X-axis with AutoLayout, AutoLayout will always try to center it within the contentView, not in the outer container (i.e. the UITableViewCell). Then, when you add a disclosure indicator to the UITableViewCell, the contentView automatically gets shrinked in its width because the cell makes space for the disclosure indicator and wants to prevent you from adding UI elements in the right area that is reserved for the disclosure indicator.
Now, you have a few options around this:
you can edit the constraint directly and add a constant to it (which has to be the same value that the label gets shifted when you'd remove the indicator)
don't use the default disclosure indicator (i.e. don't tick the checkbox in Storyboards) and just add a UIImageView with an image that looks identical.
To not be bound to any constants you can calculate the difference in widths of frame and contentView.frame. So first create an outlet collection like so:
#property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *centerConstraintsToOffset;
Then add the center constraints that you want to be centered horizontally in cell to that outlet collection:
And finally add this code to your cell:
override func layoutSubviews() {
super.layoutSubviews()
for constraint in centerConstraintsToOffset {
constraint.constant = (frame.size.width - contentView.frame.size.width) / 2.0
}
}
This also gives you flexibility of adding or removing cell accessories on the go, and your views will always be perfectly center aligned. Even if you remove the accessory at all.
Pavel's answer fixed the issue for me. After creating the IBOutlet collection as his answer demonstrates, here is his code example edited for Swift 3:
override func layoutSubviews() {
super.layoutSubviews()
for constraint: NSLayoutConstraint in self.centerConstraintsToOffset {
constraint.constant = (frame.size.width - contentView.frame.size.width) / 2.0
}
}
As you've already noticed, adding a 'UITableViewCellAccessoryDisclosureIndicator' will shrink the space allotted for your cell's contentView. Another solution that doesn't require a custom indicator or guessing at an offset would be to programmatically add a UILabel to the root view of the cell, not the contentView. For example:
#property UILabel *label;
// ...
- (void)layoutSubviews
{
[super layoutSubviews];
[self.label removeFromSuperview];
self.label = [[UILabel alloc] init];
self.label.text = #"Motorsport";
[self.label sizeToFit];
label.center = CGPointMake(self.center.x, self.size.height/2);
[self addSubview:self.label];
}

Xcode / iOS / iPhone How to set event to UILabel

Please give me an advice.
I create UILabels programmatically (dynamic).
Is there is a chance to add Event to them?
What I want by steps:
I create UILabel;
I set Event to it; (NSNotification?)
When I do some action (rotate, for example) I want that Label is changed or removed. An extended example: I create Labels and when I rotate device I want that part of them (which with attached Events) disappear in animation.
I create a lot of Labels, so I can't just set them global variables. And I can't set them tags unlimited. So UILabel *label = (UILabel*)[self.view viewWithTag:labelCount not a solution. Getting element by 'viewWithTag' has one more trouble - when set animation to that element and that element already in animation happens collision - they plays one over other...
I create Labels like this:
CGRect *labelFrame = CGRectMake(left, top, width, height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.text = #"Hi, I'm one of these army of labels";
[self.view addSubview:label];
PS: Sorry for English.
I assume you have a UIViewController that has a bunch of labels. I would recommend an IBOutletConnection for storing a reference to all of your labels (assuming storyboard).
//You will need to connect all of these labels through Interface Builder.
#property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;
In one of the following rotation methods (Detect rotation changes in iOS) do your rearranging.
//Called whenever orientation changes
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
for (UILabel *label in self.labels) {
//Make each label disappear here.
}
}

Resources