I have a scrollView which has an origin.y = -200, size.height = 200 and contentSize.height = 270.
On the main view I have a tableView with the touch of a button. the scrollView animates from the top to origin.y = 64. Because the scrollView and tableView share the same color background, I want a shadow outside the the scrollView to make the views separate.
But when I set the scrollView.layer.masksToBounds = YES it automatically expands to its full contentSize which is 270. If I comment out the maskToBounds I get no shadow. What am I missing? Thank you in advance
_addScroller.layer.shadowColor = [UIColor blackColor].CGColor;
_addScroller.layer.shadowOffset = CGSizeMake(0, 2);
_addScroller.layer.shadowOpacity = .7f;
_addScroller.layer.shadowRadius = 2;
// _addScroller.layer.masksToBounds = NO;
CGPathRef path = [UIBezierPath bezierPathWithRect:_addScroller.frame].CGPath;
_addScroller.layer.shadowPath = path;
This is with masksToBounds = NO;
it automatically sets the frame.height to the contentSize.height
What if you add the scrollview inside another view (of the same size as the scrollview, i.e. 200px height) and set the container view's shadow and maskToBounds?
I think a similar situation is in this question.
Related
Im new to IOS development , so when i change the width of a UITextFeild dynamically i want the button below to shift up .
i tried using the constrains but it doesn't seem to shift dynamically .
(IBAction)selectStatus:(id)sender {
CGRect frameRect = _textViewDevices.frame;
frameRect.size.height = 10;
self.textViewDevices.frame = frameRect;
any good example of how to achieve that ?
I want to achieve something like the Relative positioning in android .
current box before any action
Try calling layoutIfNeeded after the modifications:
- (IBAction)selectStatus:(id)sender {
CGRect frameRect = _textViewDevices.frame;
frameRect.size.height = 10;
self.textViewDevices.frame = frameRect;
[self.view layoutIfNeeded];
}
If you have a height constraint on the text view, try to set its constant instead of setting the frame height:
- (IBAction)selectStatus:(id)sender {
self.textViewHeightConstraint.constant = 10;
[self.view layoutIfNeeded];
}
Programmatically when the first violet field changes in height, to make all the below views stay next to it, you should update the frame.origin.y properly.
So, for example, the status label should be reframed like this
CGRect frame = statusLabel.frame;
frame.origin.y = firstField.origin.y + firstField.size.height + 5;
statusLabel.frame = frame;
And the same for all below views (I've supposed 5 pixels of space between views)
I have a UIView and I've added a UIView inside it as a subview. I'm hoping to expand the subview to fill a portion of the view, as a percentage, for example:
subview.bounds.width = view.bound.width(num1/max)
where num1 changes every second until it reaches max.
A UIView's frame can't be partially changed - to get the result you're looking for, you need to create a new frame, manipulate it, then attach it to your subview, like so:
let newFrame = subview.frame
newFrame.size.width = view.frame.width * num1 / max
subview.frame = newFrame
I have a detail view where I want to show a title, subtitle and content for articles. I want to be able to use HTML to format the text, so I've used a UIWebView for showing the article body. This works perfectly.
How ever, all of this, is inside a UIScrollView, so my issue is that I have to calculate the height of the UIScrollView?
This is how it works today:
And this is how it looks like in Storyboard:
So what I need to find out, is what is the correct code and syntax to calculate the correct height of the UIScrollView? Amongst several things, I tried [self.scrollView sizeToFit] without luck.
EDIT: Apparently it sets the correct heights with the code below, but seems like the view never updates.
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
// get height of content in webview
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight;"] floatValue];
// set new frame height
CGRect frame = webView.frame;
frame.size.height = height;
webView.frame = frame; // webview is now correct height
// set new frame height for scrollview (parent of webview)
CGRect scrollFrame = self.scrollView.frame;
scrollFrame.size.height = webView.frame.origin.y + height;
self.scrollView.frame = scrollFrame;
// log to console for cross checking
NSLog(#"new frame: %f, scrollview frame: %f", scrollFrame.size.height, self.scrollView.frame.size.height);
}
The console reports the apparently correct height:
new frame: 582.000000, scrollview frame: 582.000000
And a quick check in Photoshop as well, this seems to be correct:
The summed value of green and blue area is 582 pixels, but the scrollview still just scrolls the 504 pixel area from below the navigation bar to the bottom of the screen (to the bottom of the tab bar).
The webview has internally a scrollview. You can query its size by webview.scrollView.contentSize. You have to wait with this until the webview has finished rendering.
So, in the -webViewDidFinishLoad: delegate method you can get the optimal height of the webView through webView.scrollView.contentSize.height. You can then resize the webView to this height and layout the other views appropriately. If all of this is done in a custom view, the proper way of doing this would probably be to just call [theView setNeedsLayout] and override -layoutSubviews in theView.
You also should set webView.scrollView.alwaysBounceVertically to NO.
I solved the problem.
First of all, just expand the UIWebView to a height higher than the content ever will be (e.g. 2000 pixels).
The delegate method code that makes the magic happen
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
// set height for webiew
webView.autoresizesSubviews = NO;
webView.translatesAutoresizingMaskIntoConstraints = YES;
webView.scrollView.autoresizesSubviews = NO;
webView.scrollView.translatesAutoresizingMaskIntoConstraints = YES;
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('content').clientHeight;"] floatValue] + 80; // +80 for tabbar and spacing
CGRect frame = webView.frame;
frame.size.height = height;
webView.frame = frame;
// fix height of scroll view as well
self.scrollView.translatesAutoresizingMaskIntoConstraints = YES;
self.scrollView.contentSize = CGSizeMake(320, (self.webView.frame.origin.y + self.webView.frame.size.height));
}
So normally when you have a view and you want to add a drop shadow I do something like this:
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:diptic.bounds];
diptic.layer.masksToBounds = NO;
diptic.layer.shadowColor = [UIColor blackColor].CGColor;
diptic.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
diptic.layer.shadowRadius = 10;
diptic.layer.shadowOpacity = .5f;
diptic.layer.shadowPath = shadowPath.CGPath;
Where diptic is my UIScrollView.
The problem is that because diptic is a scroll view, that I have content in the scroll view that I don't want to be on the screen until they scroll to it, so I want to have masksToBounds set to YES but if I do that then I can't see my shadow..
you can see the description of the dartboard ("A basically new dartboard...") I want to be hidden until the user scrolls to it. Also, the rounded corners of the diptic isn't clipping the square corners on the top of the image.
Is there any way to be selective of what is masked and what isn't?
Put diptic into another view with the same bounds, that way you can mask diptic to bounds but apply the shadow to the containing view that wouldn't have its content masked to its bounds.
I have a view containing a UITableView. On view did load, I set the table's tableFooterView to be a view I create by hand (a UIView containing a button). When the view appears, I adjust the frame of the tableFooterView:
CGRect tableFrame = self.myTableView.frame;
CGRect originalFrame = self.tableFooter.frame;
CGSize imageSize = CGSizeMake(40.0f, 40.0f);
CGRect footerFrame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, tableFrame.size.width, imageSize.height);
self.tableFooter.frame = footerFrame;
CGRect nextFrame = CGRectMake((footerFrame.size.width - imageSize.width) / 2.0f, 0.0f, imageSize.width, imageSize.height);
self.tableFooterButton.frame = nextFrame;
On iOS7, this works the way I want -- when I scroll to the bottom of the table, I see my footer view. On iOS6, when I scroll to the bottom of the table, the footer view is still offscreen (I can pull the scroll view and see it, but it resets offscreen when I release).
What should I do differently?
After change the frame of table footer, try this:
self.myTableView.tableFooterView = nil ;
self.myTableView.tableFooterView = self.tableFooter ;