How to add UIView subviews in a view aligned like 3x3 table - objective-c

I have created a main View and in that I add 9 UIMyView that I subclassed from UIView.
I add instances of that UIMyViews using NSBundle to load the nib file and addSubview in the main view controller, of course all of these views are appeared in the upper left corner of the super view one hiding the other and the last one to be visible, which either I need to position them somehow using points in super view or create something like a table and add to its cells the UIMyViews?!
Imagine you have a table and put 9 deck cards positioned 3 rows and 3 columns (9 cards).
This is what I need to achieve.
[EDIT]
Check this image: http://www.wpclipart.com/recreation/games/card_deck/cards_symbols/playing_card_symbols.png
I just need 3 rows and 3 columns.
Anyone can suggest best practice for this kind of operation?
Afterwards just to have in mind I want to implement various animations and effects on these UIMyViews while draged, touched and reordering etc.
Thank you.

If you want to loop through all the views and position them in a grid, you can so like this:
// Array of 9 views
NSArray *views = [NSArray arrayWithObjects:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], nil];
int count = 0;
int maxPerRow = 3;
int row = 0;
float spacing = 100.0;
for(UIView *view in views)
{
// Only added to tell the difference between views, make sure to import QuartzCore or remove the next 3 lines
view.backgroundColor = [UIColor colorWithRed:.5 green:0 blue:0 alpha:1];
view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor whiteColor].CGColor;
// position view
view.frame = CGRectMake(count*spacing, row * spacing, view.frame.size.width, view.frame.size.height);
[self.view addSubview:view];
if(count % maxPerRow == maxPerRow-1)
{
count = 0;
row++;
}
else
{
count++;
}
}
That will give you something like this:

If you want to add them all manually you just set the frame of each of the subviews prior to adding to the main view:
CustomView *view = [[CustomView alloc] initWithFrame:CGRectMake(...)];
There are "smarter" ways to approach this than setting everything manually. iOS6 contains some features for this and there's also gridviews made such as:
GMGridView
DTGridView
AQGridView

Related

make non-move UITextField on background of UITableView

How to make UITextField on background of UITableView?
I want to set textField in the top and when i scroll tableView my textField don't move.
Something like this
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 100, 30)];
self.textField.backgroundColor = [UIColor redColor];
self.tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:NSStringFromClass([TableViewCell class])];
[self.view addSubview:self.textField];
}
You have another solution - besides one suggested in comments:
You can create UIViewController with tableView and text field. Assign delegate and dataSource to the view controller. Pin text field to the top and you have your desired behaviour. You will have to connect tableView with IBOutlets.
If you want to set textField in the top change textField frame
From
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 100, 30)];
To
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
self.textField.backgroundColor = [UIColor redColor];
[self.view addSubview:self.textField];
Also another solution is add textField to navigation item title.I tried this and it works fine.
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
textField.backgroundColor = [UIColor redColor];
[self.view addSubview:textField];
self.navigationItem.titleView = self.textField;

ScrollView is not working when try to add as a subview

I have problems with my scrollView! I have a view called Rootview and I wanna add three subviews to a scrollview. When I add the scrollview as a subview the middle view and the three subviews disappear. This is my code.. The Rootview (whole window) and the scrollview are outlets.
UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 218, 2024, 218)];
[middleView setBackgroundColor: [UIColor yellowColor]];
[RootView addSubview:middleView];
//create new views for sap systems beginning from left
UIView *firstSystem1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 218)];
[firstSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:firstSystem1];
UIView *secondSystem1 = [[UIView alloc] initWithFrame:CGRectMake(305, 0, 300, 218)];
[secondSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:secondSystem1];
UIView *thirdSystem1 = [[UIView alloc] initWithFrame:CGRectMake(610, 0, 300, 218)];
[thirdSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:thirdSystem1];
CGSize scrollViewContentSize1 = CGSizeMake(2024, 218);
[middleScrollView setContentSize:scrollViewContentSize1];
[middleScrollView addSubview:middleView];
Can anyone help me with that?
The problem is that you aren't adding your middleScrollView anywhere
change this
[RootView addSubview:middleView];
for this
[RootView addSubview:middleScrollView];

Disable interaction on upper view

I have two views in my program. One is statusView, it's alpha is set to 0.8f. And below it is tableView. How do I disable statusView such way, that clicking on it would not click tableView. Settings userInteractionEnabled = false; didn't help.
Here is how i add views.
[self.view addSubview:_builded.view]; // obj with my tableView
info = [[Info alloc] init];
[self.view addSubview:info.view]; // statusView
If you add a subview like the following, the tableView underneath would not catch the user interaction.
UITableView *testTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
testTableView.delegate = self;
testTableView.dataSource = self;
[self.view addSubview:testTableView];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
statusView.backgroundColor = [UIColor redColor];
statusView.alpha = 0.8;
[self.view addSubview:statusView];

UiimageView adapted in a uiview

I have a little problem with my UIImageView.
I want the content of UIImageView don't overflow to UIView.
When I tryed this code, I have this result, but I won't a image in area striped.
UIView *view = [[UIView alloc] init];
[view setFrame:CGRectMake(10, 0, 300, 100)];
[self.view addSubview:view];
UIImageView *viewimageArticle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
viewimageArticle.image =[UIImage imageNamed:#"article1.jpeg"];
viewimageArticle.contentMode = UIViewContentModeScaleAspectFill;
viewimageArticle.frame = CGRectMake(0, 0, 320, 100);
[view addSubview:viewimageArticle];
I tryed a many properties, "contentMode" and "autorezingMask" but i don't have good result.
Thank you !
Use
view.clipsToBounds = YES;
This causes its subviews to be clipped to the receivers bounds.
set clipsToBounds = YES on view
I am not 100% clear of what you want to achieve, but what you want may be UIViewContentModeScaleAspectFit instead of UIViewContentModeScaleAspectFill.
Also, you are creating the UIImageView frame in 300x100 first and resetting to 320x100. I believe the resetting part is not your intention.

Using subviews in scrollerview iPhone

I am trying to add uipickerview in uiscrollview, but it is not being shown properly, just a black square.
Further, I have to add 1 more uipickerview, some labels and some buttons.
The code I am using is as under:
-(void)loadView {
[super loadView];
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroller.pagingEnabled = YES;
NSInteger numberOfViews = 2;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.height;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
// awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroller addSubview:awesomeView];
[scroller addSubview:pView];
}
scroller.contentSize = CGSizeMake(self.view.frame.size.width , self.view.frame.size.height * numberOfViews);
[self.view addSubview:scroller];
}
what can be the issue?
Are you putting data in the picker view? I mean, if you just put the picker view without data on it you are not going to see anything on it.
You need to add the delegate and the datasource and put some data in your picker!
pView.delegate = self;
pView.datasource = self;
and then you have to implemente at least:
numberOfComponentsInPickerView:
numberOfRowsInComponent:
titleForRow:
make sure you are implementing uipickerviewdelegate methods. its normal that it will show just the black square because your pickerview cant find the data and thus does not display anything.
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
Change height to 216 .. UIPickerView has fixed height of 216 ..only.. you can't change it..