UIScrollView not scrolling in xcode 4? - objective-c

For some reason UIScrollView is not working. It did work before I add the content but after it stop working. I'm stuck can someone help me out!
Here is my code
This is the code for my UIScrollView
#interface EditAccountViewController : UIViewController {
IBOutlet UIScrollView *svScroller;
}
Here is my view Load code
- (void)viewDidLoad
{
[super viewDidLoad];
[svScroller setScrollEnabled:YES];
[svScroller setContentSize:CGSizeMake(320, 930)];
}
Here is my objects view
View
Scroll View
Image View
View
View
Toolbar

It works also with that.
To my understanding "autolayout" set constraint after loading the view so the problem could be that you set your constrains on viewDidLoad and just after that "autolayout" changes that. Try to set this constrains on viewDidAppear.
Get it from: scrollview xcode4.5 autolayout. Thanks.

Always keep in mind that the content view needs to be larger than the UIScrollView instance frame size. If it isn't, it will not enable scrolling.
This applies to UITableView as well. The scrolling for UITableView will not appear unless there are more rows to be displayed than actually are.

I resolved the issue. I just bump the height in [svScroller setContentSize:CGSizeMake(320, 1500)]; and It's working fine.

Related

How can I create a ViewController which has a fixed MapKit above a scrolling TableView?

How can I create a ViewController which has a fixed MapKit above a scrolling TableView?
I am coding this using Storyboards, and while I am an experienced developer this is my first time using iOS, XCode and Objective-C. I currently have a method which kind of works, but the method which I am currently using works by setting the MapKit as the section header of the table view inside a UITableViewController and does not look right. (It works since as I have only one section that MapKit section will always be fixed while I scroll the table view.)
The code that does this looks like this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 200.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 200)];
return mapView;
}
Unfortunately doing this means that the scrolling bar appears alongside the Map as well as the table view. Instead, I wish the scrolling bars to only be as tall as the table view.
There are other answers around the site about doing stuff like this, but none of them mention the height of the scroll bars (and the elegance of the code.)
Any ideas?
The link you provided is exactly what you should do, but in the Xib file also include the MKMapView above the table view, and set the outlets properly for that as well(And implementing the proper delegate protocols). The scroll bar will only go as high as the table view because it's a subview of the actual table, and, generally, this is the accepted way of doing things on the UI unless you want to do absolutely everything programmatically.
In this case you can take a separate UIView and place it after Exit. Put your mapView inside that UIView and do the required. Also draw an outlet for your tableView(myTableView) as well as the UIView(viewWithMapView). Now add this in your viewDidLoad() :-
[_myTableView setTableHeaderView:_viewWithMapView];
This will add your mapView as tableView header instead of section view header.

Embed ImageView in ScrollView with Auto Layout on iOS 6

I am trying to make very simple element with new iOS 6 SDK with auto layout.
I have an ImageView and Embed it in ScrollView. (everything build with Interface Builder). The .png file is set and imageView mode is set to "Top Left".
Implementation:
#import "ImaginariumViewController.h"
#interface ImaginariumViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
#implementation ImaginariumViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame =
CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
}
#end
When I run the app, the image is not scrolled. Doing all the same with auto layout turned off (with struts and springs), I have working scrolling.
I guess the problem is with constraints. Could anybody help me, please?
I just encountered the same issue in a tutorial that I was updating. I attempted programmatically deleting constraints, cursing, and banging my head against the wall - no luck.
About 5 minutes ago, however, I tried something that had fixed another issue I encountered, and, ta da! UIScrollView is working again! The solution was to move the old code that sets the UIScrollView contentSize property into an implementation of viewDidAppear, rather than viewDidLoad:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.theScroller.contentSize=CGSizeMake(200.0,2000.0);
}
I hope this helps someone else encountering some of the headaches that have appeared with Auto Layout.
Autolayout can be very confusing at first. You actually don't want to set the contentSize of the scrollview anywhere. With a pure autolayout approach the scrollview sets its own content size. See the section on autolayout and UIScrollView in the iOS 6 release notes:
The constraints on the subviews of the scroll view must result in a
size to fill, which is then interpreted as the content size of the
scroll view. (This should not be confused with the
intrinsicContentSize method used for Auto Layout.)
Note that this means that the constraints on the subviews of the scrollview must set explicit widths and heights and not use widths that vary based on aspects of the scrollview.
The second error here is that you set the frame of the UIImageView to the size of the image. With autolayout this is also unnecessary. The UIImageView actually has an intrinsicContentSize which is the size of the underlying image. (To change this you should set constraints for width and height with a high priority) That means that with auto layout to place an image in a scrollview and have it scroll the correct code should be the following:
** nothing at all!!! **
But theres still something you need to watch out for that could cause you to have an image that appears not to scroll and the hint is in the aforelinked release notes:
Note that you can make a subview of the scroll view appear to float
(not scroll) over the other scrolling content by creating constraints
between the view and a view outside the scroll view’s subtree, such as
the scroll view’s superview.
i.e. if you set constraints in interface builder and constrain the image view to a view above the scrollview in the hierarchy it will affect how the view appears to scroll. Mad!
Happy Coding...

NSWindow not resizing to fit NSView - SOMETIMES?

I'm having trouble resizing an NSWindow to fit an NSView. It must be a logic error as it works but not for one action.
I have one NSWindow which is empty, and 3 NSViews with components and are different sizes.
With the following code I resize the NSWindow to fit the NSView and display it:
[_window setContentSize:_mainView.frame.size];
[_window setContentView:_mainView];
This code works fine.
However in one NSView I have a Back button, and while this displays the correct NSView in the NSWindow, it does not re-size it back. As an example the initial window is a certain size, I click to switch to another view and it resizes correctly, I press the back button, the NSView is displayed but the window stays the same size?
Can anyone explain to me why when I switch back to the original NSView, it doesn't resize the NSWindow?
Thanks in advance everyone. This is the complete code I have:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[_window setContentSize:_mainView.frame.size];
[_window setContentView:_mainView];
}
- (IBAction)switchSubtractionView:(id)sender {
[_window setContentSize:_subtractionView.frame.size];
[_window setContentView:_subtractionView];
}
- (IBAction)switchAdditionView:(id)sender {
[_window setContentSize:_additionView.frame.size];
[_window setContentView:_additionView];
}
// -----------------------------------------------------------------------
// THE FOLLOWING METHOD DISPLAYS THE NEW VIEW CORRECTLY BUT DOESN'T RESIZE
// -----------------------------------------------------------------------
- (IBAction)switchMainMenu:(id)sender {
[_window setContentSize:_mainView.frame.size];
[_window setContentView:_mainView];
}
Thanks in advance everyone.
EDIT: it seems to me that when getting VIEW.frame.size, if this is repeatedly used, it loses its values? This seems very strange behaviour to me?
A window's content view must always be sized to fill the window's content area. Therefore, when you set the window's content size, you effectively change the size of the current content view. This happens just before you switch the content view, so you are changing the size of the old view.
Try setting the content view to a new, disposable NSView before changing the content size, and then setting the new content view.

Putting a custom view into a UITableView

I have a regular-style UITableView—the one that has a white background and gray horizontal lines to separate the rows.
I have another custom UIView that is just a 100x100 rectangle filled with redColor.
How can I put the latter into the former, such that it appears over the horizontal lines, but is still a “part” of the table view in the sense that when I scroll the table view around, the red view scrolls with it? In fact, I should also be able to put my finger on the red area and scroll the table view.
Once again, if the red view is placed to overlap some horizontal lines, it should appear over the lines. Sadly, when I just add the red view as a subview to the table view, the horizontal lines go over the red view; see this screenshot.
How can this be accomplished?
The correct place to handle the stacking order of your red square is in the layoutSubviews method. The table view sends itself layoutSubviews any time it adds or removes subviews (and at other times).
You need to make a subclass of UITableView that has a reference to the red square:
#interface MyTableView : UITableView
#property (weak, readonly) IBOutlet UIView *redSquare;
#end
You can initialize redSquare in whatever way you want. I just put it in my nib along with the table view, and moved it to be a subview of the table view in awakeFromNib:
#implementation MyTableView
#synthesize redSquare = _redSquare;
- (void)awakeFromNib {
[self addSubview:self.redSquare];
}
Anyway, to actually make sure the red square is always on top of the table cells and grid lines, override layoutSubviews like this:
- (void)layoutSubviews {
[super layoutSubviews];
[self bringSubviewToFront:self.redSquare];
}
EDIT
If you are trying to add the view a above the lines (hide the lines) try to use – bringSubviewToFront: to take it to the front of the table view.
[self.tableView bringSubviewToFront:redView];
ELSE
Add the view to the self.tableView.tableHeaderView this will place it above the table view and will scroll with the table view.
UIView *redView = [[UIView alloc]init];
redView.frame = //set the frame
redView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = redView;
Good Luck
Just add this view to UITableView as subview:
[tableView addSubview:myRedView];
See userInteractionEnabled property in order to handle interaction and scrolling.
Make your view a subview of any normal subview of the UITableView: a header, a footer or any UITableViewCell.
I think what you've described can best be achieved using a UITableViewCell or even a subview of the header. A cell has the inherent ability to scroll the table and can be customized any way you like it. It's essentially a view.
In your situation, for example, you may want the red box to appear by default at the top of the table. You would make the first cell a 'red box' cell, where you would insert your red box into the cell's content view.
So, your problem is basically, that the UITableViewCells of a UITableView are added as Subviews dynamically, and you cannot control wether they are added in front of or behind your view.
So to keep your view at the front, you need to get it back there every time cells may be added, which occurs when the UITableView scrolls.
I would suggest you try adding your custom view as a subview and then override -scrollViewDidScroll like so:
- (void)scrollViewDidScroll {
[super scrollViewDidScroll];
// myCustomView is your custom view referenced in an IVar
[self.tableView bringSubviewToFront:myCustomView];
}
Edit your viewWillAppear delegate with these lines
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(30,30, 100, 100)];
redView.backgroundColor=[UIColor redColor];
[self.tableView addSubview:redView];

stop tableviewcontroller scrolling

I am adding a textview to UITableviewController's view and now I want to stop the scrolling of view of UITableviewController.
But I am unable to do so. As its a view so no enable and disable scrolling properties like table view are existing.
Any ideas???
self.tableView.scrollEnabled = NO;
where self is your UITableViewController