C# - Adding views programmatically of unknown size - uiview

I'm trying to add some views programmatically and from what I understand, you need to declare the size of view.
For example:
UITableView table = new UITableView(new CGRect(50, 0, 200, 100));
this.View.AddSubview(table);
But what if you don't know the size? What if this is dependent on dynamic data? Will the container simply expand as necessary?
I am trying to build a shopping cart interface. There are 3 main sections:
a TableView which contains the headers for the shopping basket (e.g. "Product Name", "Qty", etc).
A dynamic size Table which its size is dependent on the number of products contained in it.
Some checkout buttons underneath the cart.
Here is a really rough sample of what I'd like to see. The section in white is the dynamic area which will grow depending on how many items there are.
Currently what I'm doing is setting up one UITableView for the headings:
tableForHeaders = new UITableView(new CGRect(0, 0, screenWidth, 50));
tableForHeaders.SeparatorStyle = UITableViewCellSeparatorStyle.None;
tableForHeaders.Source = new UIShoppingCartHeaderTable.TableSource(headerRow);
this.View.AddSubview(tableForHeaders);
Then I have created another table for the line items (I won't bore you with my implementation of TableSource):
tableForRows = new UITableView(new CGRect(0, 50, screenWidth, this.View.Frame.GetMaxY()));
tableForRows.SeparatorStyle = UITableViewCellSeparatorStyle.None;
this.View.AddSubview(tableForRows);
And finally, I'm then trying to add the buttons in:
btnCheckout = new UIButton(new CGRect(0, 50 + tableForRows.ContentSize.Height, screenWidth, 50))
{
BackgroundColor = UIColor.Red
};
btnCheckout.SetTitle("Checkout", UIControlState.Normal);
btnCheckout.SetTitleColor(UIColor.White, UIControlState.Normal);
btnContinue = new UIButton(new CGRect(0, 100 + tableForRows.ContentSize.Height, screenWidth, 50))
{
BackgroundColor = UIColor.Red
};
btnContinue.SetTitle("Continue", UIControlState.Normal);
btnContinue.SetTitleColor(UIColor.White, UIControlState.Normal);
this.View.AddSubview(btnCheckout);
this.View.AddSubview(btnContinue);
I set the height of tableForRows to be GetMaxY() - as I thought this might be the best way to leave it as big as possible. I'm then trying to set the height of the buttons to be ContentSize.Height. Again, I'm sure I've done this wrong, just not sure how to logically do this.

if you want full size view in your app then its like this
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HT [UIScreen mainScreen].bounds.size.height
tableViewForActivities=[[UITableView alloc]init];
tableViewForActivities.frame=CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HT);
suppose width=50 hight =50
if you want view in centre then by giving width it will adjust automatically within any iOS device
view= [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-width)/2, 0, width, width)];
Thank you

you can just put this for
view2 = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-width)/2, CGRectGetMaxY(view.frame), width, width)];
i think this will help you

Related

Text of textview is overlapping under textview

My question is simple and I think it is possible but I cant find it.
My design is like this
I give corner radius to textview and also set textContainerInset for padding from right and left both side
txtview_msg.textContainerInset = UIEdgeInsetsMake(5, 10, 10, 5)
Now my problem is like for first 2 line and last 2 line my text goes beneath textview so I need some solution for it.
Please help me into this. Thank you
try this code
UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, txtview_msg.frame.size.height - 30)];
txtview_msg.leftView = paddingView1;
txtview_msg.leftViewMode = UITextFieldViewModeAlways;
UIView *paddingView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, txtview_msg.frame.size.height - 30)];
txtview_msg.leftView = paddingView2;
txtview_msg.rightViewMode = UITextFieldViewModeAlways;
You can create at first a view of same size how much you given, put cornerRadius how much you given in textview now. than create textview viewSize - corner radius. place the textview exactly in middle of view. set the constraints from that view only, than it will work perfect how you want.
check this attached screenshot:

IOS - Sticky UICollectionView Header (like Apple App Store)

I am trying to create a header like the one in the App Store. Where when the user drags down, the header stays in place and only the cells scroll down. But when the user drags up, both the header and cells scroll up.
What I have done so far is set my collectionView background view to the image I want to stick to the top and set contentInsets of the collectionView below the image. This way when the user drags down, the image will stay in place. But when the user drags up, my cells go over the image (I want the image to scroll up with the cells). I think I am going about this wrong. Thanks.
UIView *back = [UIView new];
UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
backImage.image = [UIImage imageWithData:coverData];
[back addSubview:backImage];
self.collectionView.backgroundView = back;
[self.collectionView setContentInset:UIEdgeInsetsMake(300, 0, 0, 0)];
EDIT
I used the tutorial from this question but this does the opposite of what I want, and I am unsure of how to change that code to fit my needs
Ok I got it. If you are using the tutorial posted from the linked question-
Change
origin.y = MIN(
MAX(
contentOffset.y,
(CGRectGetMinY(firstCellAttrs.frame) - headerHeight)
),
(CGRectGetMaxY(lastCellAttrs.frame) - headerHeight)
);
To This
origin.y = MIN(
contentOffset.y,
(CGRectGetMinY(firstCellAttrs.frame) - headerHeight)
);

UIScrollView with UITableView not scrollable

My App (Xamarin/monotouch for iPhone) consists of three UITableView which I add to a UIScrollView (all is done programtically)
Each of the tables is not scrollable
myTableView.ScrollEnabled = false;
I calculate the height needed for x number of items and set it in runtime.
myTableView.Frame = new RectangleF (0, 27, 320, myTableViewHeight);
The problem is that the UIScrollView is not scrollable:
int caclulateScrollViewHeight = myListHeight + oListHeight + 104;
scrollView.ContentSize = new SizeF (320, caclulateScrollViewHeight);
scrollView.Frame = new RectangleF (0, 135, 320, caclulateScrollViewHeight);
scrollView.DelaysContentTouches = true;
scrollView.CanCancelContentTouches = false;
this.View.Add (scrollView);
Any idea what can cause that?
Don't set your scrollView frame height the same as your calculated view height. The frame is the box on screen that you want it to occupy; generally you'll want to keep this less than or equal to screen size. If your content size matches your frame size, it won't scroll (because it won't need to).
scrollView.Frame = new RectangleF (0, 135, 320, 200);

Programmatically create subview that covers the top 50% of the iPhone screen

I have a subview and I have been customizing its size by using the frame property and setting its value to the CGRectMake function's parameter values.
I have slowly but surely been changing the CGRectMake parameters and re-running the app to get the subview to the correct position on the screen but I know there has to be an easier way.
Here is what I am currently doing:
UIImageView *halfView = [[UIImageView alloc]initWithImage:image];
[self.view addSubview:halfView];
halfView.frame = CGRectMake(0, 0, 320, 270);
Is there a way that I can stop having to manually enter those 4 parameters into CGRectMake, and just set it to the top 50% of the screen?
Here is what I want the subview to look like on the iphone's screen:
halfView.frame = CGRectMake(0, 0, 320, self.view.bounds.size.height/2);
you just take the height and divide it by two
You should also probably change 320 to self.view.bounds.size.width
I suggest reading this post to really get a grasp of UIViews and working with them:
UIView frame, bounds and center

If no Image, change row cell height

I'm using SDWebImage. I'm pulling the images in correctly from a web service API, but if the API I'm getting the response from doesn't have an image ("null"), I want to realign my Table View Cell.
ViewController.m
if ([fL.images count] == 0) {
//trying to figure out what to put here to change row height
}
else {
}
WebListCell.m
- (void)layoutSubviews {
[super layoutSubviews];
// (X, Y, Width, Height)
self.publishedLabel.frame = CGRectMake(300, 210, 20, 20);
self.imageView.frame = CGRectMake(0, 0, 320, 180);
self.headlineLabel.frame = CGRectMake(10, 210, 290, 40);
self.descriptionLabel.frame = CGRectMake(10, 250, 290, 30);
self.premiumLabel.frame = CGRectMake(260, 2, 60, 20);
}
My problem is I can't find a good "if" statement for when there is no image and I want to realign my Table View Cell.
I think I need to do something in WeblistCell.m and heightForRowAtIndexPath for this, but I'm stumped.
Any help would be appreciated!
Will post any necessary code needed!
Looks like you are pretty close to the solution for yourself. The key is the method -heightForRowAtIndexPath: - this needs to return the different height for the case when the row doesn't have an image. I'm not familiar with SDWebImage but if you are loading images from a web service of some kind, you should know which row (index) an image is for when you try to load it. So, if you go to retrieve an image and there isn't one (null), add that index to a mutable array to track missing image. Then your -heightForRowAtIndexpath: method just needs to check if the row property of the indexPath parameter exists in that array.

Resources