Resize UICollectionView Height - xcode

I'm trying to resize a UICollectionView height by setting it to 0 when the view controller is loaded, then increasing its size with an animation when a button is pressed. I've tried a few different things but it doesn't change in size at all. Here's all the different things I've tried to change its height to 0:
CGRect bounds = [self.collectionView bounds];
[self.collectionView setBounds:CGRectMake(bounds.origin.x,
bounds.origin.y,
bounds.size.width,
bounds.size.height - 100)];
....
CGRect frame = [self.collectionView frame];
[self.collectionView setFrame:CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height - 100)];
....
CGRect frame = self.collectionView.frame;
frame.size.height -= 100;
self.collectionView.frame = frame;
....
self.collectionView.clipsToBounds = YES;
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

If you are using Interface Builder for UICollectionView initialisation, switch off "Use Autolayout" from the File Inspector in the xib file where the your CollectionView is created. Then you can change the height with setFrame or setBounds methods.

You can avoid disabling Autolayout by creating an outlet for the height constraint and then adjusting the constraint's constant in code.
Outlet
#IBOutlet weak var collectionViewVerticalConstraint: NSLayoutConstraint!
Adjustment
collectionViewVerticalConstraint.constant = 0

Related

Label on UIButton not laid out correctly with autolayout

I have an app which I'm updating to auto layout and size classes and I'm getting some weird behaviour with the label on a button.
The button should be a circle and have a label in the centre. I'm implementing my own subclass so I can reuse it.
Here's the storyboard:
and the code for the class which extends UIButton:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setBackgroundImage:[UIImage imageNamed:#"selected-green"] forState:UIControlStateHighlighted ];
self.layer.borderColor = [UIColor tlbWhiteColor].CGColor;
self.layer.borderWidth = 10;
}
return self;
}
-(void) layoutSubviews {
self.layer.cornerRadius = self.frame.size.width / 2;
}
With this the appears as expected but there is no label. On debugging I see that the frame of the label has 0 height and width. So I extended layoutSubviews:
-(void) layoutSubviews {
self.layer.cornerRadius = self.frame.size.width / 2;
if (self.titleLabel.frame.size.width == 0) {
[self.titleLabel sizeToFit];
[self setNeedsLayout];
[self layoutIfNeeded];
}
}
The label then appears, but it's in the wrong place:
The only extra info I can offer is that in Reveal the button has weird height and width constraints added:
The titleInsets are all at 0.
Help hugely appreciated.
I don't think you need the layoutSubviews override. I think it's a hack and it's hiding your real issue.
What are the constraints on the 'Go' label? How are you centering it in the UIButton? Also what is the height constraint on the label?
My suggestion would be to put both the UIButton and the Go label inside a container view and centre the label inside the container view. The container view should have the same height and width as the UIButton inside it.
Also are you changing the frames of the views programmatically somewhere in the app? Those weird constraints you talk about in reveal point to that.
As per your requirements:
- (void)viewDidLoad {
[super viewDidLoad];
view_1.layer.cornerRadius = 10; //view_1 is white color
view_1.layer.masksToBounds = YES;
view_2.layer.cornerRadius = _view_2.frame.size.width/2;//view_2 is green color
view_2.layer.masksToBounds = YES;
}
output
Ok, this was a real school boy error.
When the docs said 'The implementation of this method is empty before iOS 6' I for some reason took this to mean there's no need to call super on layoutSubviews.
So the fix was:
-(void) layoutSubviews {
[super layoutSubviews]; // <<<<< THIS WAS MISSING
self.layer.cornerRadius = self.frame.size.width / 2;
self.layer.masksToBounds = YES;
}

UIScrollview not scrolling what am i doing wrong

I have an issue where i am adding a UIScrolview to my UIViewController, but my UIScrollview doesnt want to scroll. I want it to work the same way that it works on a UITableViewController.
Here i initialize my UIScrollview
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
scroll.contentSize = CGSizeMake(320, 800);
scroll.backgroundColor = [UIColor redColor];
scroll.showsHorizontalScrollIndicator = YES;
scroll.scrollEnabled = YES;
scroll.userInteractionEnabled=YES;
[self.view addSubview:scroll];
Here i add my content to the scrollview:
UILabel *memberNo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
memberNo.text = #"Member No";
memberNo.font = [UIFont boldSystemFontOfSize:16];
memberNo.textColor = [UIColor limeColor];
[scroll addSubview:memberNo];
memberText = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
memberText.placeholder = #"Member No";
memberText.borderStyle = UITextBorderStyleRoundedRect;
memberText.text = #"1111111";
[scroll addSubview:memberText];
Try this
set content size more than the screen size and it will scroll whether there is more content or not
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
HorizontalScroll
scroll.contentSize = CGSizeMake(320, 900);
VerticalScroll
scroll.contentSize = CGSizeMake(340, 800);
As others have said, the scroll view's size (specifically, its bounds.size) must be smaller than its contentSize for it to actually scroll.
You probably want your scroll view to fit inside its superview, so try setting its frame to the superview's bounds:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
The content is the same size as the scrollview: there is nothing to scroll.
The scrollview needs to contain a view that is bigger than it's own size.
Try setting the contentSize to be larger than the frame. For example:
scroll.contentSize = CGSizeMake(1000, 800);
If that doesn't work, then check where you are setting the content size. Where are you initializing the UIScrollView? Try moving the above line to viewWillAppear or viewDidLayoutSubviews.
frame of the scroll view refers to the size of the scroll view itself whereas content size is the size for the view which is to be shown in the scroll view. If the size of view to be shown in scroll view is having smaller size than the scroll views frame size, then it won't at all scroll, but if you set content size more then scroll view has to scroll to show the 'extra' size more than its own frame size.
e.g. If height of scroll view is 100 and you have set the height factor in content size equal to 150, then these extra 50 pixels will be scrollable.
Good Luck!

Implementing zooming in CALayer

I'm trying to implement zoom on a CALayer in a layered-hosting NSVIew. The NSView with the layer (size of the layer: 1024x768) is inside an NSScrollView. What I did to implement the zoom is the following
- (IBAction)zoomOut:(NSButton *)sender {
CALayer *layer = self.editorView.layer;
CGRect frame = layer.frame;
frame.size.width = frame.size.width / 2;
frame.size.height = frame.size.height / 2;
layer.frame = frame;
}
- (IBAction)zoomIn:(NSButton *)sender {
CALayer *layer = self.editorView.layer;
CGRect frame = layer.frame;
frame.size.width = frame.size.width * 2;
frame.size.height = frame.size.height * 2;
layer.frame = frame;
}
After clicking on the button for zooming in or zooming out in the NSView, the problem is that whenever i scroll the NSView (remember that I put the NSView inside an NSScrollView, and that the NSView is bigger than the NSScrollView), the CALayer resizes automatically to the original size (i.e. 1024x768). I cannot figure out the reason.
Any kind of help would be much appreciated.
+ (CALayer *)layer alias backing store (I call it root layer) resizes with the view, there is no way to prevent this.

CGGradient isn't visible (not using interface builder) and UIButtons can't be triggered

I have created a view that contains a CGGradient:
// Bar ContextRef
CGRect bar = CGRectMake(0, screenHeight-staffAlignment, screenWidth, barWidth);
CGContextRef barContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(barContext);
CGContextClipToRect(barContext,bar);
// Bar GradientRef
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[16] = { 1.0,1.0,1.0,0.0, 0.0,0.0,0.0,1.0, 0.0,0.0,0.0,1.0, 1.0,1.0,1.0,0.0};
CGFloat locations[4] = {0.95,0.85,0.15,0.05};
size_t count = 4;
CGGradientRef gradientRef = CGGradientCreateWithColorComponents(colorSpace, components, locations, count);
// Draw Bar
CGPoint startPoint = {0.0,0.0};
CGPoint endPoint = {screenWidth,0.0};
CGContextDrawLinearGradient(barContext, gradientRef, startPoint, endPoint, 0);
CGContextRestoreGState(barContext);
This code is called in the drawRect method of the UIView. I then use a UIViewController to access the created view.
- (void)loadView {
MainPageView *mpView = [[MainPageView alloc] initWithFrame:[window bounds]];
[self setView:mpView];
[mpView release];
}
and displayed on the screen through the appDelegate:
mpViewController = [[MainPageViewController alloc] init];
[window addSubview:[mpViewController view]];
[window makeKeyAndVisible];
The UIView contains more objects, such as UIButtons, that are visible. I am assuming because they are added as a subview. But I can't work out how to add the CGGradient as a subview? Does it need to be? Is there another reason CGGradient is not visible?
I also don't get the functionality on the UIButtons. I guess that is because of where I have added the UIButtons to the view. Do the buttons need to be added in the UIViewController or the appDelegate to have functionality. Sorry to ask what would seem like simple questions but I am trying to accomplish the programming without the Interface Builder and material on that is scarce. If anyone could point me in the right direction on both these problems I would really appreciate it.
Thanks!
The functionality on the buttons was lost because the frame was too large but the buttons were still visible because the background was clearColor

Making NSScroller larger

I'm making a Cocoa application optimized for external touch sensitive screens, so I want to make a large scollbar in an NSScrollView. I've tried just to resize the scrollers frame, but both drawing and mouse events only happens in the scroll area, it doesn't get wider (it's the vertical scroller). This is the code I'm using (from the subclassed NSScrollView):
- (void)tile{
[super tile];
NSScroller *vertScroll = [self verticalScroller];
NSRect vertScrollFrame = [vertScroll frame];
NSView *content = [self contentView];
NSRect contentFrame = [content frame];
contentFrame.size.width -= 50;
vertScrollFrame.origin.x -= 50;
vertScrollFrame.size.width += 50;
[vertScroll setFrame:vertScrollFrame];
[content setFrame:contentFrame];
}
I've tried to subclass the NSScroller, but I don't know how I would extent it.
Does your customized scroll view exist in a nib/xib? If so, have you set the scroll view's class identity in Interface Builder to the name of your custom class? It's found in the "Identity" tab of the Inspector window.

Resources