I am using IOS 5. I am a very beginner in IOS. In my project i have 2 xib files. One is a UIView and other is a UIViewcotroller. The UIViewcontroller is having 3 views(TopBar,ContentArea,BottomBar). uiview also has 3 subviews. I added UIView as subview of UIViewController. My requirement is change the UIView height when i hide topbar only, bottombar only, topbar and bottombar.
Hope you can help me.
Thanks in advance.
set the views as properties,
#property (nonatomic, retain)UIView *element1_p1;
then when you need to change the size, change the frame,
self.element1_p1.frame = CGRectMake(center.x,
center.y,
elementWidth, elementHeight);
I recommend doing an animation for the change of height, so it feels better UX
great tutorial for changing views properties with animation
UIView has method as setFrame to resize the view.
Read this and this - you will get an idea how subviews respond to parent view's frame changes.
Related
Good day,
I have a question about programmatically adding UITableView inside UIScrollView. The structure is like that:
UIView
UIScrollView
UIView(contentView)
UIImageView
UITableView
How can I add the UITableView programmatically inside UIScrollView so when I scroll horizontally to switch between UIImageView and UITableView. Note I am using Autolayout so I need to add constraints also. If it is going to be easier with storyboard please let me know how to do it with storyboard instead of programmatically
I used SwipeView it is pretty easy to use 5 minutes installation and did what I was looking for.
I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is,
I placed UIScrollView inside the main view in Storyboard
I placed UITextView inside UIScrollView in Storyboard and disabled Scrolling Enabled
I set constraints (leading, trailing, top, bottom) on UIScrollView
I set constraints (top, leading, trailing, height) on UITextView
I created IBOutlet of height constraint of UITextView
I set a text (a lot of text which can cause scrolling) on UITextView in viewDidLoad()
I set a height constraint of UITextView with the code below. I have tried it right after setting text in viewDidLoad() and viewDidLayoutSubviews() with no luck
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height;
UITextView is getting its height, but UIScrollView isn't. Is there anything I've missed?
After a few days of research and getting my hands dirty with UIScrollView + UITextView + Auto Layout, I successfully got a fully working UIScrollView. I want to share my solution just in case someone might stuck on the same situation.
Add UIScrollView inside the main view in Storyboard
Add UIView inside the UIScrollView
Add UITextView inside the UIView (the view added in step 2)
Make sure "Scrolling Enabled" of UITextView is unchecked
Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
Add UITextView height constraint IBOutlet on the ViewController. #property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; and connect it in Storyboard
Change the UITextView height constraint programmatically. self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
After all of these 10 steps, you'll get fully working UIScrollView with UITextView inside and be happy.
It seems that this question has an answer that has worked for a number of people. However, it should be noted that the documentation states:
Placing a text view inside of a scroll view. Text views handle their own scrolling. You should not embed text view objects in scroll views. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Before trying to put a UITextView in a UIScrollView you should consider if this is really necessary. If the text view is part of a complex layout within the scroll view, then note the use of the UIView container in the accepted answer.
See also: Scroll Views Inside Scroll Views
If you noticed that the Auto Layout is still complaining about the height of the UIScrollView-
The problem here is that by default UITextView has checked the “Scrolling Enabled” in the IB.
So find that checkbox and uncheck it.
Sodbileg's solution above works, but you do not need all of his steps. I got my scrollview to scroll with a text view inside by following his steps through step 7. For step 8, I added the 4 constraints but not the height (Don't forget the bottom constraint!!). I did not do the rest.
I am not sure, but it seems that the height of the textview adjusts automatically instead of us having to manually change it.
I have UIViewController with UIScrollView in it.
UIScrollView contains multiple UIViewControllers (something like PageControl Apple example).
Each inner UIViewController in UIScrollView is able to handle the change of UIInterfaceOrientation and to redraw it's view.
But when the UIViewControllers are in UIScrollView the rotations events never reaches inner UIViewControllers.
How can I make those inner UIViewControllers to handle rotation events?
Thanks in advance.
Only a viewController in a viewController structure (like tabbarcontroller or navigationcontroller) gets (and handles) rotation events. If you just added the views of some viewControllers, they won't actually rotate, but they will change their framesize accordingly to the new size of the main view. In general you should use autoresizingMasks on every view. Read about them in the documentation.
But in a scrollView there is probably no autolayouting possible. You could overwrite layoutSubviews: in your main view to handle the layout of all scrollView subviews based on the scrollView frame.
Perhaps you can detect when the device is rotated, then using the transform property apply the necessary rotation programmatically using CGAffineTransformMakeRotation(-M_PI_2), for example...
Does anyone know a way to, in a storyboard, load a UIView's contents from another nib? I know I can do this easily with code, but I am trying to figure out how to do more in IB.
I have a storyboard with my main UI layout, I have a UIScrollView and I want to design its contents in IB. The only way I could figure out how to do this was to design the UIView in its own .nib, but then my issue is, how do I load the nib without coding it to do so? Is this even possible? It doesn't seem too far fetched to me.
I'm assuming you simply want to lay out your UIScrollView in IB, that a .nib is mentioned because that was an approach you were exploring, but if you could do this strictly in your storyboard that would be acceptable, if not preferable:
First, create a new file in Xcode that is a subclass of UIScrollView.
In your storyboard, drag a UIScrollView out to the scene (viewcontroller) where you want to display this scroll view.
In the Identity inspector, set the Custom Class of the UIScrollView to your subclass of UIScrollView.
Create an outlet for this UIScrollView by ctrl+dragging the UIScrollView into the .h file of the ViewController subclass it's displayed in. Name it something like myScrollView
In your ViewController's -viewDidLoad method, set the contentSize property of the UIScrollView to whatever size you want it to be. So it will look something like:
self.myScrollView.contentSize = CGSizeMake(800,800);
Now, drag out UI objects to your UIScrollView and design.
IMPORTANT: To create outlets to these objects is a little tricky. Let's say you've dragged out a UILabel. You need to manually go into your UIScrollView subclass and add to the .h
#property (nonatomic, weak) IBOutlet UILabel* myLabel;
and to the .m
#synthesize myLabel = _myLabel;
Now you need to get your outline view on screen along with your storyboard and ctrl+drag FROM YOUR SCROLL VIEW TO YOUR LABEL to create an outlet. This is kind of the reverse of what you're used to.
Now you can reference that outlet from within the viewcontroller or the scrollview subclass . For instance, in the viewcontroller -viewDidLoad you could say:
self.scrollView.myLabel.text = #"Hello World";
HTH!
If what you want is to edit inside a scrollview from IB, it's a pain, but doable.
Have a look at my answer on this question.
Add a generic UIView in the IB, setting its custom class to the name of your nib file.
Replace GradientControl with the name of your nib file (minus the '.xib').
Imagine, there is a UIViewController with a UIScrollView in it. At the top of the view there is an UIImageView, some UILabels and other things. Furthermore, there is a UITableView which content is Dynamic Prototypes. I attach a picture to make it clear:
I haven't got a static amount of cells in the UITableView so it could be scrollable. My problem is the following: the UITableView scrolls in itself but I want to scroll the whole View. What is the best possibility to do that?
Possible solutions I've founded today
1) The first thing is: I create a UITableViewController and declare a header section in which I include all my labels, images etc. programmatically (I would love to use the interface builder for that...)
2) Another solution is to calculate the height of the view. I tried the best to do it like this way - but: without success. If this is the best way to do that: Can anybody give an example?
I would ditch the UIScrollView and just use a UITableView. You can add a UIView object as the tableHeaderView of the UITableView just by dragging it in in Interface Builder. Now since everything is part of the UITableView hierarchy, everything will scroll together as expected.
You could also try setting delaysContentTouches to NO on your scrollView. Depending on your setup, this may make the scroll view respond to the touch first instead of the table view.
From Apples UIScrollView Docs:
delaysContentTouches
A Boolean value that determines whether the scroll view delays the
handling of touch-down gestures.
#property(nonatomic) BOOL delaysContentTouches
Discussion
If the value of this property is YES, the scroll view delays handling
the touch-down gesture until it can determine if scrolling is the
intent. If the value is NO , the scroll view immediately calls
touchesShouldBegin:withEvent:inContentView:. The default
value is YES.
You'll have to (as you've mentioned) add the UIView containing the image and buttons to the actual UITableView. Embedding it in the scroll view will produce the undesired behavior that you're seeing.
I would recommend returning the UIView as the header view for the first section of your table view. You can do this by implementing the UITableViewDelegate method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
If you maintain an IBOutlet to the view containing your image/labels, you can return it here.
this is same demo i hope its helps you from iphone sorce code library
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html
thank you