Displaying UItableViewController inside UIPopoverController - objective-c

I am dispalying a UITableViewController inside a UIPopoverController. Everything works fine expect that the size of the TableViewController is very large even thought there are only 3-4 cells. How can I make it so the popover controller's height is exactly the same as tableview's height?

You could use UIPopoverController's method - (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated or use the property on UIViewController #property(nonatomic, readwrite) CGSize contentSizeForViewInPopover to achieve this.

Override the contentSizeForViewInPopover of your UITableViewController like this:
- (CGSize)contentSizeForViewInPopover {
// Currently no way to obtain the width dynamically before viewWillAppear.
CGFloat width = 200.0;
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}

Related

Make UITextView scroll action hide a UILabel

Basically, what I want to do is detect when a user scrolls inside a text view, and then hide a label (Smoothly fade out, if possible). (The label indicates to scroll to view the rest of the text, but I don't want it to still show after the user has done so.)
If you could include in your answer the code used in the h/m files, it would be greatly appreciated.
Updated code for future reference:
.h
#interface myViewController : UIViewController
#property(nonatomic,retain) IBOutlet UILabel *label;
.m
#synthesize label;
- (void)scrollViewDidScroll:(UIScrollView *)textView
{
[UIView animateWithDuration:1.0 animations:^{
label.alpha = 0;
}];
}
Then make sure to set the UITextView delegate to self.
You can use UIScrollView's delegate method – scrollViewDidScroll: to detect that the user has scrolled, and fade out your label with a UIView animation block, like so:
[UIView animateWithDuration:1.0 animations:^{
label.alpha = 0
}];

Why won't this code allocate a subView?

This is my code where I'm trying to create a subView... I'm using XCode4 with StoryBoards. The app is crashing on the 2nd line where it is allocating subView with EXC_BAD_ACCESS. vFrame has valid content. What is wrong with this? (I'm using XCode4 with Storyboards, btw).
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect vFrame = CGRectMake(60,100,200,200);
subView = [[UIView alloc] initWithFrame:vFrame];
subView.backgroundColor = [UIColor redColor];
[self.view addSubview: subView];
}
UPDATE: definition for subView:
#interface PreferencesViewController : UIViewController {
UIView *subView;
}
#property (nonatomic, retain) UIView *subView;
#end
That really should work. Was your view controller (PreferencesViewController) properly alloc'd and init'd? Did you #synthesize your subview?
Although it shouldn't matter, you could try using floats for the CGRect (add a .0 to the end of each).
Try moving your sub view instantiation code to the method viewWillAppear of your main view. This guarantees everything has been initialized.
The problem was the UIViewController was farkled...don't know how, but once it was replaced, it worked like a champ! Thank you all for your responses...

(iOS) How can I Reuse an UIImageView in another View?

I have a view controller with a UIScrollView with a heap of UIImageViews as subviews. There is a segue to another view controller but i want to reuse the UIImages that are already in memory rather than load them again for efficiency's sake. But if I point my new UIImageView to an existing one, nothing shows up.
I had to use an intermediate pointer variable (imageViewPointer) as if I assign it directly in the prepareForSegue method, the NSLog's from each view controller would show different addresses. This workaround NSLogs show the same address but still nothing shows up.
If I use reassign the .image property instead, the image shows up but the images from the first view controller are load asynchronously so the .image property might change.
In the first view controller
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NextViewController *nextViewController = [segue destinationViewController];
nextViewController.imageViewPointer = [[myScrollView subviews] objectAtIndex:myIndex];
NSLog(#"%#", [[myScrollView subviews] objectAtIndex:myIndex]);
}
In the second view controller
#interface nextViewController : UIViewController
#property (retain, nonatomic) IBOutlet UIImageView *imageView;
#property (retain, nonatomic) UIImageView *imageViewPointer;
#implementation nextViewController
#synthesize imageView, imageViewPointer;
{
self.imageView = self.imageViewPointer;
// [self.imageView setNeedsDisplay]; Doesn't help
NSLog(#"IMAGE VIEW POINTER = %#", self.imageViewPointer);
NSLog(#"IMAGE VIEW = %#", self.imageView);
[super viewDidLoad];
}
If you simply assign a view like that, it won't appear in the view hierarchy of your current view controller's view. You'll need to do something like:
[self.view addSubview: imageView];
However, I wouldn't recommend that. It is better to just assign the image of the view to the image of the other view.
self.imageView.image = self.imageViewPointer.image;
Either you reuse the UIImage or the method you are doing is a bit wrong as you are assigning the imageViewPointer to imageView which is(imageViewPointer) not a subview of your view controller's view. So add subview imageViewPointer or imageView after assigning imageViewPointer.

How to repaint UIView when text in UILabel changes?

I've a subclass of UIView with a UILabel with is added as subview to the view. In the layoutSubviews method the position and height of the UILabel is calculated. My problem is, that this height should be recalculated, when the text of the UILabel changes. The text is inserted into the label from the UIViewController, so the view does not know when this happen.
You can subclass UILabel to override setText: like this:
#implementation MyLabel
- (void)setText:(NSString *)text {
[super setText:text];
[self.superview setNeedsLayout];
}

NSScrollView doesn't show scrollbars even although the content is large enough

I have this class:
Header:
#interface vcMain : NSWindowController {
IBOutlet NSView *scroll;
}
#property (retain) IBOutlet NSView *scroll;
-(IBAction)test:(id)sender;
#end
Source:
#implementation vcMain
#synthesize scroll;
-(IBAction)test:(id)sender {
vItem *item = [[vItem alloc] initWithNibName:#"vItem" bundle:nil];
NSView *view = [item view];
[view setFrame:NSMakeRect(0, 0, 300, 600)];
[view setAutoresizingMask:( NSViewHeightSizable) ];
[scroll addSubview:view];
}
#end
*scroll is a Custom View in a Bordered Scroll View in the Content View of a Window.
vItem is a ViewController subclass with some things on it to identify its position.
Problem: When resizing my vcMain from the default 300x600 to 150x300, I don't see any scrollbars.
What am I doing wrong?
Tom
Solved
It was simple, actually. Resizing the view apparently also resized the subview so it wasn't neccessary to show the scrollbars - however, as the elements in the subview didn't move, I didn't notice that the subview was resizing.
Solved by correcting the resizing of the view.