UIScrollView with UITableView not scrollable - ios

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);

Related

Set Frame size of CollectionView

I have made a semi rounded collectionView. I want to set my collectionView frame that take only half of screen but it take Full screen so I am unable to do any things.
Please let me know how to set frame for that UICollectionView.
DSCircularLayout *circularLayout = [[DSCircularLayout alloc] init];
[circularLayout initWithCentre:CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT)
radius:165
itemSize:CGSizeMake(ITEM_WIDTH, ITEM_HEIGHT)
andAngularSpacing:60];
[circularLayout setStartAngle:M_PI endAngle:0];
circularLayout.mirrorX = NO;
circularLayout.mirrorY = NO;
circularLayout.rotateItems = YES;
circularLayout.scrollDirection = UICollectionViewScrollPositionRight;
// self.menuCollection.init(f: CGRect, collectionViewLayout layout: UICollectionViewLayout);
[self.menuCollection setCollectionViewLayout:circularLayout];
menuCollection is my collectionView which i want to set frame.
thanks
Assuming you are not using auto-layout, and your collection view's parent view is full screen size, you can set it as follows.
CGFloat size = self.menuCollection.superview.bounds.size;
self.menuCollection.frame = CGRectMake(0.0, size.height / 4, size.width, size.height / 2);

How can I scroll the text on image in xamarin.ios

hear is my code:
var label = new UILabel { Text = text };
label.SetNeedsLayout();
float width = 125;
SizeF size = (SizeF)(label.Text).StringSize(label.Font, new SizeF(width, 100),UILineBreakMode.TailTruncation);
label.Lines = 5;
label.Frame = new RectangleF(20, 40, size.Width, size.Height);
var image = new UIImageView();
image.Image = new UIImage("bluesticky.png");
image.Frame = new CGRect(0, 0, 180, 220);
this.AddSubview(image);
this.AddSubview(label);
In my code UILabel text is placed on UIImage. If the text is larger than the image I want to scroll the text on image.How can I get like that If I have large amount of text. Thanks in advance
its difficult with UIlable - You can do but you will also need scrollview.
Use UITextView instead. Change background color to clear editable property to false. It will automatically manage scroll.
///Update:
To disable dragging of image when Your textView is scrolling, you can disable Dragging event of UIImageview when scroll started and Enable again when scroll ended.
mytextview1.DecelerationStarted += (sender, e) =>
{
//Disable dragging of Image.
};
mytextview1.DecelerationEnded += (sender, e) =>
{
//Enable dragging of Image.
};

C# - Adding views programmatically of unknown size

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

Scroll a view with 20 text boxes horizontally using UIScrollView

I have to implement 20 text boxes horizontally. How can I add a scroll bar to see all text boxes when I scroll to the left.
please try following code:
UIScrollView *scrlView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
scrlView.scrollEnabled=YES;
int x=5;
int y=5;
for (int i=0;i<20;i++ ) {
UITextField *txtField=[[UITextField alloc] initWithFrame:CGRectMake(x,y , 100,40 )];
txtField.tag=i;
txtField.delegate=self;
[scrlView addSubview:txtField];
x+=105;
}
scrlView.contentSize=CGSizeMake(x, 50);
[self.view addSubview:scrlView];
i hope this code will help you.
You can find plenty of questions about this topic (i.e., Calculate the contentsize of scrollview), but nonetheless here is my solution:
You want to use the UIScrollView for the task. Using it is rather simple in fact.
First you need to initialise and display the UIScrollView somewhere in your screen. I am going to assume that you are in a UIViewController, and that you want to create a scroll view of 300px width and 200px height. For this, you can do the following:
UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
[[self view] addSubview:aScrollView];
[aScrollView setScrollEnabled:YES];
Now its time to place the content inside of it.
The key idea here to take into consideration is the following. While the 'UIScrollView' has a size itself, it also has an internal size for it's content, which is essentially the are that is scrollable.
Let's suppose that you have 20 items, each of which has a size of '40px width', and '200px height'. I am also going to suppose that you will want to leave a space of 10px between each text box. Thus, the total scrollable width for the area that we want to have inside the 'UIScrollView' is the following: (20 (items) * (40 (size of each item) + 10 (distance between each item))) -10. We shall then do the following:
[aScrollView setContentSize:CGSizeMake((20*(40 + 10))-10), 200];
All right, we are almost done. Now all we have to do is add each of the text items to the scroll view. Instead of doing it one by one, I am going to assume that you have them inside a collection such as a NSArray (probably an NSMutableArray).
NSArray *textFields; // the array with the collection of text boxes
int x = 0;
for (UITextField *aTextField in textFields){
[aTextField setFrame:CGRectMake(x, 0, 40, 200)];
[[self view] addSubview:aTextField];
x += 40 + 10; // incrementing the X coordinate to correctly place the next item
}
That's it!

Applying transform to UITextView - prevent content resizing

When I apply a rotation transform to a UITextView and then click inside to begin editing, it appears that the content size is automatically being made wider. The new width of the content view is the width of the rotated view's bounding box. For example, given a text box of width 500 and height 400, and rotated by 30 degrees, the new content width would be:
(500 * cos(30)) + (400 * sin(30)) = 633
Or graphically:
Interestingly, if you are already editing the text view and THEN apply the transform, then it appears that no modification is made to the content size. So it appears that sometime around the start of text editing, the text view looks at its frame property and adjusts the content size based on the frame width. I imagine the solution to this is to tell it to use the bounds property instead, however I don't know where to do this, as I'm not sure exactly where the text view is deciding to modify the content size.
I have googled but can't seem to find any references to using transformed UITextViews. Does anybody have any ideas about this?
EDIT (button action from test project):
- (IBAction)rotateButtonTapped:(id)sender {
if (CGAffineTransformIsIdentity(self.textView.transform)) {
self.textView.transform = CGAffineTransformMakeRotation(30.0 * M_PI / 180.0);
}
else {
self.textView.transform = CGAffineTransformIdentity;
}
NSLog(#"contentsize: %.0f, %.0f", textView.contentSize.width, textView.contentSize.height);
}
I was also stuck with this problem.
The only solution which I found was to create an instance of UIView and add the UITextView as a subview. Then you can rotate the instance of UIView and UITextView will work just fine.
UITextView *myTextView = [[UITextView alloc] init];
[myTextView setFrame:CGRectMake(0, 0, 100, 100)];
UIView *myRotateView = [[UIView alloc] init];
[myRotateView setFrame:CGRectMake(20, 20, 100, 100)];
[myRotateView setBackgroundColor:[UIColor clearColor]];
[myRotateView addSubview:myTextView];
myRotateView.transform = CGAffineTransformMakeRotation(0.8);
[[self view] addSubview:myRotateView];
Have you tried applying the rotation by doing a layer transform rather than a transform on the view?
#import <QuartzCore/QuartzCore.h>
mytextField.layer.transform = CATransform3DMakeRotation (angle, 0, 0, 1);
This might be enough to trick whatever broken logic exists inside the core text field code.

Resources