I want to use label to create a 15 * 15 matrix world.How to loop the row and column?
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 15, 15)];
myLabel.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:myLabel];
Assuming you need to create 50 Label of size 18x98
int x = 0;
int y = 0;
for (int i = 0; i < 50; i++) {
if (20 * x > self.view.frame.size.width) {
x = 0;
y++;
}
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * x, 100 * y, 18, 98)];
myLabel.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:myLabel];
x++;
}
Related
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
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) { ...
What I am trying to do is align views in a grid that are in an array. I have created the views and the array.
for (int i = 0; i < [directoryContents count]; i++){
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,120)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
button.accessibilityIdentifier = [directoryContents objectAtIndex:i];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 20)];
label.textAlignment = NSTextAlignmentCenter;
label.text = [directoryContents objectAtIndex:i];
[containerView addSubview:button];
[containerView addSubview:label];
[self.view addSubview:containerView];
[_containerViewArray addObject:containerView];
}
[self layoutViews:_containerViewArray inView:self.view];
What I have working is aligning them next to each other the problem I am having is I need to have them wrap down instead of going off the edge. What I am doing to algin them is this
- (void)layoutViews:(NSArray *)views inView:(UIView *)contentView
{
NSInteger overallWidth = 0;
for ( UIView *view in views ) {
overallWidth += view.frame.size.width;
}
CGSize contentSize = contentView.frame.size;
CGFloat startOffset = (contentSize.height - overallWidth)/2.0;
CGFloat offset = startOffset;
for ( UIView *view in views ) {
CGRect frame = view.frame;
frame.origin.x = offset;
view.frame = frame;
offset += view.frame.size.width;
}
}
If we assumed that we have an array of sections (your views) and you need to layout 3 item per row the view has size 98*98 and spacing 6.5px horizontally and 8px Vertically
for (int i = 0; i< [sectionsArray count]; i++) {
int row = (int)i/3;
int col = i%3;
float x = 6.5 + (104.5*col);
float y = 8 + (106*row);
UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 98, 98)];
sectionView.backgroundColor = [UIColor clearColor];
//button
UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[sectionButton setTintColor:[UIColor grayColor]];
sectionButton.frame = CGRectMake(0, 0, 98, 98);
sectionButton.tag = i+100;
[sectionButton addTarget:self action:#selector(showSectionDetail:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:sectionButton];
//adding title
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, 98, 20)];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.font=[UIFont fontWithName:#"Helvetica" size:14];
titleLabel.textColor = [UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0];
titleLabel.text = [[sectionsArray objectAtIndex:i] objectForKey:#"Title"];
[sectionView addSubview:titleLabel];
[titleLabel release];
[scroll addSubview:sectionView];
[sectionView release];
}
int numberOfRows;
if ([sectionsArray count]/3.0f == 0) {
numberOfRows = [sectionsArray count]/3;
}else{
numberOfRows = [sectionsArray count]/3 +1;
}
scroll setContentSize:CGSizeMake(320, numberOfRows*106);
Here's what I used until the UICollectionView was released, easy to modify to accommodate for how many rows you want in the grid, and the size of the views.
NSUInteger buttonWidth = 100;
NSUInteger buttonHeight = 100;
NSUInteger space = 5;
float scrollContentY = (ceilf((float)imageNames.count / 3.0f) * (buttonHeight + space));
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
int row = idx / 3;
int column = idx % 3;
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
view.frame = CGRectMake((buttonWidth + space) * column + space, (buttonHeight + space) * row + space , buttonWidth, buttonHeight);
[view setTag:(NSInteger)idx];
[self.scrollView addSubview:view];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, scrollContentY)];
}];
Mind you, this might not be advisable if you have a large array to enumerate because this doesn't have any kind of reusable views. It will alloc/init a view for every item in the array.
I have this code and I want to have my button as a square, and also under the navigation bar,would you plase help me
Thanks in advance!
I want to have my button as a square
- (void)viewDidLoad
{
[super viewDidLoad];
int rows = 13, columns = 4;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 70*columns, 70*rows)];
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(70 * x, 28 * y, 70, 28);
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
}
}
// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];
}
-(void)buttonPressed:(UIButton *)button
{
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
}
Edit 2
my error when I using border
[button.layer setBorderWidth:1.0];
[button.layer setBorderColor:[UIColor blackColor]];
Define an integer variable and set this as button title
int rows = 13, columns = 4;
int number=1;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f,44, 70*columns, 70*rows)];
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(75 * x, 75 * y, 70, 70);
button.backgroundColor=[UIColor redColor];
[button setTitle:[NSString stringWithFormat:#"%d",number] forState:UIControlStateNormal];
button.tag=number;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
number++;
}
}
Hope this will help you to move to next screen. Button.tag gives the exact button you have selected.
You are setting your button of size 70x28, so if you want a button 70x70, you have to change button.frame = CGRectMake(70*x, 70*y, 70, 70), this way you will have square buttons.
Also, are you sure this is a NavigationBar or just a Toolbar? If you use NavigationController which uses navigationBar, Apple's own SDK will not allow you to easily draw over it.
In any case in your case, just start drawing your buttons from y = 44 down and you won't have buttons overlap the toolbar.
Try this
int rows = 13, columns = 4;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f,44, 70*columns, 70*rows)];
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(75 * x, 75 * y, 70, 70);
button.backgroundColor=[UIColor redColor];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
}
}
// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];
For using layer property you need to add QuartzCore.framework in your project.
I can't figure out what is wrong here. Please see coments above NSLog function.
-(void)loadView
{
......
int x_position = 10;
for (self.x = 0; self.x < 3; self.x++)
{
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, x_position, 300, 25)];
self.textField.tag = self.x;
// Output 0, 1, 2
NSLog(#"%d", self.x);
x_position += 40;
[self.view addSubview:self.textField];
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:#selector(showNames) forControlEvents:UIControlEventTouchDown];
[btn setTitle:#"Remove from view" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, x_position + 30, 210, 50);
[self.view addSubview:btn];
[self.textField release];
[self.view release];
}
-(void)showNames
{
while (self.x > 0) {
self.x--;
// output 2, 1, 0
NSLog(#"%d", self.x);
NSLog(#"%#", tmp);
}
}
Here is console log
<UITextField: 0x4b39410; frame = (10 90; 300 25); text = 'Ad'; clipsToBounds = YES; opaque = NO; tag = 2; layer = <CALayer: 0x4b38c30>>
<UITextField: 0x4e22320; frame = (10 50; 300 25); text = 'Asd'; clipsToBounds = YES; opaque = NO; tag = 1; layer = <CALayer: 0x4e0a4c0>>
<UIView: 0x4b32330; frame = (0 20; 320 460); layer = <CALayer: 0x4b329a0>>
I expect object at tag 0 to be UITextField, not UIView.
What is wrong here ?
Every view's tag defaults to zero, so your main UIView will have a tag of zero, and so will any other view where you didn't explicitly set the tag.
I suggest using an offset value for your tags, so you can make them all unique. For example:
#define TEXTFIELD_TAG_OFFSET 100
for (self.x = 0; self.x < 3; self.x++)
{
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, x_position, 300, 25)];
self.textField.tag = self.x + TEXTFIELD_TAG_OFFSET;
// Output 0, 1, 2
NSLog(#"%d", self.x);
x_position += 40;
[self.view addSubview:self.textField];
}
Now you can reference the Nth textfield with the tag number TEXTFIELD_TAG_OFFSET + N. That way your textfields will all have unique tags.