I am new in iOS. I am building a basic project where I have two view controllers.
First one named as tabViewController and the second one as detailViewController.
in tabViewController I have a collectionView. When I am tapping on collection view cell it is pushing to detail view controller. In detail view controller there are three image views. One is just in front of the screen (x=0, y=0, width=320, height=420). The other two imageViews are in the positions at left and right side of the one which is in front of the screen. The two imageViews are in positions as x=-330,y=0,width=320,height=420 and x=330,y=0,width=320,height=420. Consider just about iPhone5.
Now, the problem that is happening is I cant make the scrollView's position to see the image that is in left side of the screen.
The code I have written is
- (void)viewDidLoad {
[super viewDidLoad];
_myScrollView.delegate = self;
_myScrollView.pagingEnabled = YES;
_selectedImageString = [_patternArray objectAtIndex:*(_selectedIndex)];
_strImageNext = [_patternArray objectAtIndex:*(_selectedIndex)+1];
_myScrollView.bounds = CGRectMake(_myImageView2.frame.origin.x, _myScrollView.frame.origin.y, _myScrollView.frame.size.width, _myScrollView.frame.size.height);
if (*(_selectedIndex) == 0) {
_strImagePrevious = nil;
}
else{
_strImagePrevious = [_patternArray objectAtIndex:*(_selectedIndex)-1];
}
self.navigationController.navigationBar.hidden = NO;
_myImageView.image = [UIImage imageNamed:_selectedImageString];
_myImageView.frame = CGRectMake(_myScrollView.frame.origin.x, _myScrollView.frame.origin.y, _myScrollView.frame.size.width, _myScrollView.frame.size.height);
self.myScrollView.backgroundColor = [UIColor yellowColor];
_myImageView3.image = [UIImage imageNamed:_strImageNext];
if (*(_selectedIndex) == 0) {
_myImageView2.image = nil;
}
else {
_myImageView2.image = [UIImage imageNamed:_strImagePrevious];
}
[_myScrollView setScrollEnabled:YES];
[_myScrollView setContentSize:CGSizeMake((_myImageView2.frame.size.width+_myImageView2.frame.size.width+_myImageView2.frame.size.width+_myImageView.frame.size.width+ 30.0), _myScrollView.frame.size.height)];
}
Here is a sample code to move left with contentOffset. Hope it will help you.
your_scroll.contentOffset = CGPointMake(-100.0, 0.0);
Related
There are some posts, where UIWebView bottom is black and can be resolved by two simple ways.
1. Clear Background of UIWebView
2. Set Opaque to NO.
However, this only solves problem for static UIWebView which is not changing its Frame or Constraints.
In my case, I have a UIWebView, It is very simple Drag and Drop on Storyboard's ViewController's View. No Inheritance. I set Background clear and set opaque to NO.
It looks fine up to now. But when I apply animation, using Facebook POP library
1. SetOpaque = YES, With Animation is clips the UIWebView's content showing black color.
2. SetOpaque = NO, With Animation is clips the UIWebView's content showing nothing (a View or something comes in front)
-(void) setConstraint {
_topConstraintValue = self.topConstraint.constant - 200;
NSLog(#"Value is UP %f " ,_topConstraintValue);
}
- (IBAction)animateUp:(UIButton *)sender {
[self setConstraint];
[VSAnimation popChangeConstraintForBasicAnimation:self.topConstraint begin:0.5 newConstant:_topConstraintValue withDuartion:0.5 isEaseInAnimation:NO withCompletionBlock:nil];
}
-(void) setConstraintDown {
_topConstraintValue = 0;
_topConstraintValue = self.topConstraint.constant;
NSLog(#"Value is Down %f " ,_topConstraintValue);
}
- (IBAction)animateDown:(UIButton *)sender {
[self setConstraintDown];
[VSAnimation popChangeConstraintForBasicAnimation:self.topConstraint begin:0.5 newConstant:[self getAnimatableConstraint:_topConstraintValue] withDuartion:0.5 isEaseInAnimation:NO withCompletionBlock:nil];
}
-(float)getAnimatableConstraint:(float)constant{
return constant+200.0;
}
-(float)getReverseAnimatableConstraint:(float)constant{
return constant-200.0;
}
Where, method inside VS is as below
//VSAnimation.m
+(void)popChangeConstraintForBasicAnimation:(NSLayoutConstraint *) layoutConstraintToPopChange begin:(float) beginTime newConstant:(CGFloat) newConstant withDuartion:(float)duration isEaseInAnimation:(BOOL)isEaseIn withCompletionBlock:(void (^)(POPAnimation *anim, BOOL finished)) completitionBlock{
NSString *key = [NSString stringWithFormat:#"constraints_changed_%#", layoutConstraintToPopChange];
POPBasicAnimation *basicAnim = [layoutConstraintToPopChange pop_animationForKey:key];
if(basicAnim){
basicAnim.toValue = #(newConstant);
basicAnim.completionBlock = completitionBlock;
}else{
basicAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayoutConstraintConstant];
if(isEaseIn)
basicAnim.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
else
basicAnim.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
basicAnim.toValue = #(newConstant);
basicAnim.completionBlock = completitionBlock;
basicAnim.beginTime = [self timeWithDelay:beginTime];
basicAnim.delegate = self;
basicAnim.duration = duration;
[layoutConstraintToPopChange pop_addAnimation:basicAnim forKey:key];
}
}
What is this black line/transparent line at bottom.
How to stop this from increasing when one animates UIWebView.
IMAGES:
SHOWING ANIMATION UP & ANIMATION DOWN.
#VS, After reading you source code, I saw the constraint of the 2nd webview seems to be set wrongly. After I removed the height constraint WV2.height = 0.095 * height, the problem disappeared. You have to set the WV2's height constraint properly.
I have a UISplitView like design (not really UISplitView, I used View container to mimic it).
The left side container has a table view and right side container has a collection view.
If I click the tableview cell in the left container, the right side collection view will be changed accordingly.
The collection view is paginated, it always has two pages.
If I scroll to the second page of the collection view and now I click a new table view cell in the left container, it will load the correct collection view in the right container, but it is in the second page instead of the first page!
I search around and could not find a solution.
I appreciate for any advices.
Update: Added related code.
Following code is in the right container.
- (void)viewDidLoad
{
[super viewDidLoad];
[self setup];
self.collectionView.delegate = self;
self.pageControl.currentPage = 0;
[self.collectionView scrollRectToVisible:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.width) animated:NO];
}
The function setup is defined as following:
-(void)setup
{
self.collectionView.pagingEnabled = YES;
self.collectionView.directionalLockEnabled = YES;
self.collectionView.bounces = NO;
self.collectionView.showsHorizontalScrollIndicator = NO;
}
Thanks.
Why can't you write a code to scroll the scroll view to show the frame of the first page whenever user selects a row in left container.
[contentScrollView scrollRectToVisible:pageRect animated:YES];
Following is the example I have created to solve this issue :
You can download code form Here
After downloading source just add following method to MLKPageViewController.m
- (void)showPageAtIndex:(NSInteger)index
{
if( index >= self.contentVCs.count )
return;
UIView *contentView = ((UIViewController *)[self.contentVCs objectAtIndex:index]).view;
CGRect pageRect;
if( mlkPageControl.currentPage == FIRST_PAGE || mlkPageControl.currentPage == LAST_PAGE )
{
pageRect = CGRectMake( (index * CONTENT_VIEW_SPACING) + index * contentView.frame.size.width, contentScrollView.frame.origin.y , contentScrollView.frame.size.width, contentScrollView.frame.size.height);
}
else
{
pageRect = CGRectMake( (index * CONTENT_VIEW_SPACING) + index * contentView.frame.size.width - CONTENT_VIEW_SPACING, contentScrollView.frame.origin.y , contentScrollView.frame.size.width, contentScrollView.frame.size.height);
}
[contentScrollView scrollRectToVisible:pageRect animated:YES];
}
Call above method from "ViewDidLoad" method of MLKPageViewController by passing page index.
I have used GMGridView inside my app. When I change the orientation all is fine, but if I change it again and enter edit mode all the cells are shacking except the visible one in the lower-right corner.
The GmGridView is added as a subview to a controller and it also doesn't occupy the whole screen. I've tried destroying it and recreating it when a rotation notification occurs ... same behaviour. Also I have made a custom view with multiple buttons and labels that I have set as the the GMGridViewCell's contentView.
here's the code for the cellForItemAtIndex
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
GMGridViewCell *cell = [gmGridView dequeueReusableCell];
if (!cell) {
CGSize cellSize = [self GMGridView:gmGridView sizeForItemsInInterfaceOrientation:orientation];
cell = [[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, cellSize.width, cellSize.height)];
cell.deleteButtonIcon = [UIImage imageNamed:#"delete_button"];
cell.deleteButtonOffset = CGPointMake(0, 0);
}
CustomGridViewCell *gridViewContent = [CustomGridViewCell getNewGridViewCellForOrientation:orientation];
ContentData *data = [wishlistContentArray objectAtIndex:index];
[gridViewContent setuprWithContent:data];
cell.contentView = gridViewContent;
if (!gridViewContent.delegate)
gridViewContent.delegate = self;
return cell;
}
There is a bug on GMGridView when it computes the number of visible cells.
In loadRequiredItems there are these two lines of code
NSRange rangeOfPositions = [self.layoutStrategy rangeOfPositionsInBoundsFromOffset: self.contentOffset];
NSRange loadedPositionsRange = NSMakeRange(self.firstPositionLoaded, self.lastPositionLoaded - self.firstPositionLoaded);
You have to change the range so it will return the smallest number that is a multiple of the number of items on your cell.
For example if you have 2 cells on a row, right now the code will return a range of {0,5} but it should be {0,6}.
Disclaimer: I've been working too late. But, I'm determined to get through this one tonight.
I have an app where I support different color themes. The dark cell backgrounds have been problematic.
I've been poking around trying to find a formidable way to draw the accessory disclosure icon in uitableviewcells with black backgrounds.
I decided to try overriding setAccessoryType to inherit the functionality for my 50+ views:
-(void) addWhiteDisclosureImage {
UIImageView *disclosureView = (UIImageView*) [self.contentView viewWithTag:kDisclosureReplacementImageTag];
if(!disclosureView) {
[super setAccessoryType:UITableViewCellAccessoryNone];
disclosureView = [[UIImageView alloc] initWithImage:self.whiteDisclosureImage];
disclosureView.tag = kDisclosureReplacementImageTag;
disclosureView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
DebugLog(#"%f, %f", self.frame.size.width, self.frame.size.height);
[self.contentView addSubview:disclosureView];
[self.contentView bringSubviewToFront:disclosureView];
[disclosureView release];
}
}
- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType {
if(accessoryType == UITableViewCellAccessoryDisclosureIndicator) {
if ([self.viewController isKindOfClass:[ViewControllerBase class]]) {
ViewControllerBase *view = (ViewControllerBase*) self.viewController;
if(view.colorTheme && view.colorTheme.controlBackgroundColor) {
if([ViewColors colorAverage:view.colorTheme.controlBackgroundColor] < 0.2) { //substitute white disclosure indicator
[self addWhiteDisclosureImage];
return;
} else { //not dark enough
[self removeWhiteDisclosureImage];
[super setAccessoryType:accessoryType];
return;
}
} else { //no colorTheme.backgroundColor
[self removeWhiteDisclosureImage];
[super setAccessoryType:accessoryType];
return;
}
} else { //viewController is not type ViewControllerBase
[self removeWhiteDisclosureImage];
[super setAccessoryType:accessoryType];
return;
}
}
UIView *disclosureView = [self.contentView viewWithTag:kDisclosureReplacementImageTag];
if(disclosureView)
[disclosureView removeFromSuperview];
[super setAccessoryType:accessoryType];
}
This override is typically called in cellForRowAtIndexPath.
It seemed like a good option until I drill down and come back. For some cells, the cell frame will be a great deal larger than the first time through. This consistently happens to the same cell in a list of 6 that I've been testing against. There's clearly something unique about this cell: it's frame.size.
Here is the size of the cell that I log for the first tableview load (in some cases every load/reload):
320.000000, 44.000000
This is the difference in what I get for some (not all) of the cells after call to reloadData:
759.000000, 44.000000
Does anyone know why this might happen?
Update: the suspect cell's custom accessory disclosure view almost acts like it's autoresizing flag is set to none. I confirmed this by setting all to none. I say almost because I see it line up where it should be after reloadData. A split second later it moves clear over to the left (where they all end up when I opt for no autoresizing).
Don't mess around with subviews and calculating frames.
Just replace the accessoryView with the new imageView. Let iOS do the work.
I'm using altered sample code from PhotoScroller within my app. I have a table view of image thumbnails, and I can pass the array of images that populate that table to PhotoViewController. Currently, PhotoViewController starts with the first image in my array and I can scroll back and forth. This works properly as Apple's sample code.
Now What I want to do is tap a table cell with thumbnail, and start scrolling images beginning with the image in my array at that index. Ex: if I have 5 images in a table and I tap image #3, I want the first image in PhotoViewController to be that third image, and able to scroll left or right to #2 or #4. Hope this makes sense.
I see in PhotoViewController that sub views are being added per image. Any way I can tell it "jump to view #3" without destroying the other views or their overall order of appearance? Any ideas or advice is welcome. Code can be found on the iOS developer site for PhotoScroller sample code.
Ok, I'm rambling... Thanks in advance for your help!
The way I do this is to have a variable called startingPage which gets set in the initialiser of the photo view controller. Then when the pages are being created, first set the correct offset for the scroll view.
So in the PhotoScroller case that would be in loadView. Like so:
- (void)loadView
{
// Step 1: make the outer paging scroll view
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
self.view = pagingScrollView;
// Set the content offset of the scrollview
CGRect bounds = pagingScrollView.bounds;
CGPoint contentOffset = CGPointMake(bounds.size.width * startingPage, 0.0f);
[pagingScrollView setContentOffset:contentOffset animated:NO];
// Step 2: prepare to tile content
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
}