Adjust content in UIScrollView - objective-c

Hello to all I created a UIScrollView that shows a series of UIView that represent graifici according to your entry ...
The multiplication of view was performed using a FOR loop
for (NSInteger i = 0; i < value.count; i ++) {
UILabel *valueX_label = [[UILabel alloc] initWithFrame:CGRectMake(xPadding, self.scrollChart.frame.size.height-30, 50, 30)];
NSString *valueText = [value objectAtIndex:i];
valueX_label.text = [NSString stringWithFormat:#"%#", valueText];
valueX_label.textColor = [UIColor whiteColor];
valueX_label.font = [UIFont fontWithName:#"HelveticaNeue" size:9];
valueX_label.textAlignment = NSTextAlignmentCenter;
valueX_label.numberOfLines = 2;
[self.scrollChart addSubview:valueX_label];
xPadding += valueX_label.frame.size.width;
self.scrollChart.contentSize = CGSizeMake(xPadding, self.scrollChart.frame.size.height);
_backgroundBar = [[UIView alloc] initWithFrame:CGRectMake(valueX_label.center.x - 20 , 13, 40, (self.scrollChart.frame.size.height - valueX_label.frame.size.height) -10)];
_backgroundBar.backgroundColor = [UIColor grayColor];
_backgroundBar.layer.cornerRadius = 2;
_backgroundBar.alpha = .1;
[self.scrollChart addSubview:_backgroundBar];}
  Now I have a problem ... After entering the paging of ScrollView I have some display problems, ie each size page a few columns as you can see from the pictures ...
I need each page displays only six columns at a time ... you can help me in any way?
Wrong pagination
Ok pagination

Related

Constraint spacing UILabel in For Loop

I have a small problem and hope someone can help me solve it ... I searched the net and made some attempts, but I could not solve my problem.
I created a FOR loop that plays a specified number of labels obtained through an array, as you can see from the code that I posted below ...
Now my problem is how to insert constraint programmatically. I need that between a label and the other (horizontal) there is a specific space.
Example:
if I label 4 I would like these would take around the horizontal space is in the iPhone display SE both iPhone 7 Plus ..
So my question is how do I set a specific space between each label and pass in the FOR loop?
CGFloat padding = 0;
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"INFO UNIVERSITARIE",#"INFO GENERALI", #"TROVA SEDI", nil];
for (NSInteger i = 0; i < array.count; i ++) {
NSString *valueText = [array objectAtIndex:i];
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(padding *i, 0, 50, 30);
label.text = [NSString stringWithFormat:#"%#", valueText];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:12];
[label sizeToFit];
[_baseView addSubview:label];
padding = label.frame.size.width;
label.translatesAutoresizingMaskIntoConstraints = NO;
}
Researching this, I remembered that the UIStackView is our new friend, designed specifically for this problem.
NSArray *array = #[ #"INFO UNIVERSITARIE",#"INFO GENERALI", #"TROVA SEDI" ];
NSMutableArray *labels = [NSMutableArray array];
for (NSString *string in array) {
UILabel *label = [[UILabel alloc] init];
label.text = string;
[labels addObject:label];
}
UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:labels];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.spacing = 12.0;
stackView.frame = self.view.bounds;
[self.view addSubview:stackView];
Just learned about this myself, but I tried this code and it works nicely.

Reverse NSInteger in a Loop For

I'm working on this for loop and it works perfectly .. I regularly returns the list of found numbers, but I need that for loop give me back the numbers in descending order ... from 30 to 18 and not by the 18-30 as now ... Someone can 'give me a help in this?
I post the code in question
for (NSInteger i = 18; i <= 30; i += 2) {
self.axisY_Label = [[UILabel alloc] initWithFrame:CGRectMake(10, paddingValue, 35, 20)];
self.axisY_Label.text = [NSString stringWithFormat:#"%ld", (long)i];
self.axisY_Label.textColor = [UIColor whiteColor];
self.axisY_Label.font = [UIFont fontWithName:#"HelveticaNeue" size:13];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(_axisY_Label.frame.size.width, _axisY_Label.center.y, self.frame.size.width, 0.3)];
line.backgroundColor = [UIColor whiteColor];
line.alpha = .2;
[self addSubview:line];
paddingValue += _axisY_Label.frame.size.width;
[self addSubview:_axisY_Label];
}
Exchange from and to parameters and count backwards
for (NSInteger i = 30; i >= 18; i -= 2) { ...

Horizontal UIScrollView with multiple textfields to display

I'm new to Xcode. I want to make a UIScrollView with multiple pages and each page having multiple UITextFields which vary on each page. I made UIScrollView with paging enabled. Now I'm stuck at displaying textfield's on scroll pages.
Here is my code so far:
//set the paging to yes
self.scrollview.pagingEnabled = YES;
// create 5 pages
NSUInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++)
{
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//create the sub view and allocate memory
myView = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//create a label and add to the sub view
CGRect myFrame = CGRectMake(10.0f, 10.0f, 200.0f, 25.0f);
textLabel = [[UILabel alloc] initWithFrame:myFrame];
textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
textLabel.textAlignment = NSTextAlignmentLeft;
[myView addSubview:textLabel];
//create a text field and add to the sub view
myFrame.origin.y += myFrame.size.height + 10.0f;
textField = [[UITextField alloc] initWithFrame:myFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.tag = i+1;
[myView addSubview:textField];
//set the background to different color
//set the scroll view delegate to self so that we can listen for changes
self.scrollview.delegate = self;
//add the subview to the scroll view
[self.scrollview addSubview:myView];
}
//scroll horizontally
self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
self.scrollview.frame.size.height);
//we set the origin to the 1rd page
CGPoint scrollPoint = CGPointMake(self.view.frame.size.width * 0, 0);
//change the scroll view offset the the 1rd page so it will start from there
[scrollview setContentOffset:scrollPoint animated:YES];
[self.view addSubview:self.scrollview];
}
Use This code here textfieldCount is
for (int j=0; j < textFieldCount; j++)
{
myFrame.origin.y += myFrame.size.height + 10.0f*(j+1);
UITextField *textField = [[UITextField alloc] initWithFrame:myFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.tag = j+1;
[myView addSubview:textField];
}
Hope you got your answer.

Adding text to a UIScrollView

I'm creating a tape measure in a UIScrollView, it has horizontal lines from an image and I want to add the numbers programatically.
(Note the blue was to help getting the layout right)
I tried this:
for (NSInteger i = 0; i < self.maxUnits.integerValue; i++) {
static CGFloat labelWidth = 40;
CGFloat x = (halfScreen - (labelWidth/2)) + (i * self.widthOfOneFraction.floatValue * self.numberOfFractions.floatValue);
CGRect frame = CGRectMake(x-1, 41, labelWidth, 70);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = [NSString stringWithFormat:#"%d", i];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor tlbBlueColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:20.0];
[self addSubview:label];
}
Which works, but uses about 40Mb of RAM. I thought that adding the text in a CATextLayer might be more efficient, but I can't get it working:
for (NSInteger i = 0; i < self.maxUnits.integerValue; i++) {
CATextLayer *label = [[CATextLayer alloc] init];
[label setFont:#"HelveticaNeue-Light"];
[label setFontSize:20];
[label setFrame:frame];
[label setString:[NSString stringWithFormat:#"%d", i]];
[label setAlignmentMode:kCAAlignmentCenter];
[label setForegroundColor:[[UIColor whiteColor] CGColor]];
[self.layer addSublayer:label];
}
The numbers don't appear on the screen.
So my question is can I either make the first way more efficient, and if not is the second way more efficient when it works and if so how do I get it working?
I changed the way the loading worked so as to only load items on display and to load a few more as you scroll.
This seems to have totally fixed the high memory issue and even if you scroll to both ends it doesn't use the same high amount of memory. I guess there was a leak in how my original code worked.

adding buttons below the other

I want to add buttons one below the other. I have this simple code:
- (void)viewDidLoad
{
[super viewDidLoad];
for(int i=0 ; i<9 ; i++)
{
UIButton *myButton = [[UIButton alloc] init];
myButton.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height/10); //this "10" i want also dynamically
myButton.backgroundColor = [UIColor blackColor];
[self.view addSubview:myButton];
}
}
Of course i know that it will be draw one on another. But can i do it in the loop without knowing the height (becouse high will depends on how many buttons will be in the loop).
What i want to achieve:
Try This:
- (void)viewDidLoad
{
[super viewDidLoad];
int number = 10;
for(int i=0 ; i<9 ; i++)
{
UIButton *myButton = [[UIButton alloc] init];
myButton.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.size.height/number)*i + number, self.view.frame.size.width, self.view.frame.size.height/number);
myButton.backgroundColor = [UIColor blackColor];
[self.view addSubview:myButton];
}
}
you need to define the size of the buttons for the correct show, unfortunately without the size of the buttons you cannot achieve you wished because how else does the system know what sized button you'd like to show...?
- (void)viewDidLoad
{
[super viewDidLoad];
Float32 _spaceBetweenButtons = 8.f;
Float32 _offsetY = 32.f;
Float32 _buttonWidth = 300.f; // width fo button
Float32 _buttonHeight = 32.f; // height of the button
for (int i = 0 ; i < 9 ; i++) {
UIButton *_myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.f, 0.f, _buttonWidth, _buttonHeight)];
[_myButton setBackgroundColor:[UIColor blackColor]];
[_myButton setTitle:[NSString stringWithFormat:#"button #%d", i] forState:UIControlStateNormal]; // just add some text as title
[self.view addSubview:_myButton];
[_mybutton setCenter:CGPointMake(self.view.frame.size.width / 2.f, _offsetY + i * (myButton.frame.size.height + _spaceBetweenButtons))];
}
}
if you want to calculate the size of the buttons dynamically and you want to align the button's height to the view's height, here would be one way:
NSInteger _numberOfButtons = 20;
Float32 _spaceBetweenButtons = 8.f;
Float32 _calculatedHeight = (self.view.frame.size.height - (_numberOfButtons + 1 * _spaceBetweenButtons)) / _numberOfButtons;
and the method is same as above, but I'm not sure you will get good UI in case of hundreds buttons. :)
Perhaps this might work? Sorry I haven't used self.view.frame in ages, but you get the general idea.
- (void)viewDidLoad
{
[super viewDidLoad];
int number;
for(int i=0 ; i<9 ; i++)
{
UIButton *myButton = [[UIButton alloc] init];
myButton.frame = CGRectMake(i * self.view.frame.origin.x / number,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height / number); //this "10" i want also dynamically
myButton.backgroundColor = [UIColor blackColor];
[self.view addSubview:myButton];
}
}