I'm displaying an image in a UIScrollView but I want it to blend with a UIImageView which is laid behind the UIScrollView.
I'm currently trying to override - (void)drawRect:(CGRect)rect in a subclass of UIView created to display the image inside the scrollview, using either the method drawInRect:blendMode:alpha: or CGContextSetBlendMode but none worked. I think the reason is that the current context is opaque but I cannot find a workaround.
You can add UIImageView as a subclass of UIView (parent view of your scroll view).
You can change opacity of your UIImageView to match your criteria. If you just want a image as a background of your UIView you can set up background of your view and as a colour you can use [UIColor colorWithPatternImage:Your UIImage here].
Is it something you need?
//EXTENDED
I try it and both of the example below set the image as a background and when I scroll the image is not moving:
Example 1:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"newtons_cradle.jpg"]]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:#"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView];
Example 2:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:[UIImage imageNamed:#"newtons_cradle.jpg"]];
[self.view addSubview:imageView];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setContentSize:CGSizeMake(self.view.bounds.size.width, 1000.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 300, 50)];
[label setBackgroundColor:[UIColor whiteColor]];
[label setText:#"Label Test Text"];
[scrollView addSubview:label];
[self.view addSubview:scrollView]
;
Related
I have a UIView class, and in xib also I have a MainView which is a UIView. I want to add UIButton as bottomView to the View.I need to give bottom space 16 between MainView and bottomView. How do I do it?
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];
[self.bottomView setBackgroundColor:[UIColor greenColor]];
self.selectBtn= [UIButton buttonWithType: UIButtonTypeRoundedRect];
[self.selectBtn setFrame:CGRectMake(0,20,100,40)];
[self.selectBtn setTitle:#"Select" forState:UIControlStateNormal];
[self.selectBtn addTarget:self action:#selector(selectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.bottomView addSubview:self.selectBtn];
[self addSubview:self.bottomView];
Expected result
Output from above code
Please find you have added button on this frame
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];
Here you have made the y as height + 30 so it is not visible to the view.
You should use this.
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height - 40, self.MainView.bounds.size.width, 40.0)];
Also if you want 16 pixel gap you should the view above it with the same approach.
Please let me know if you need any more help
I have navigation bar with custom view on it. UIImageView and UILabel are subviews of titleView. And I add UITapGestureRecognizer to titleView to make it show another UIViewController when the user taps on it. When you tap on titleView other UIViewController opens. But when you click on back button in my titleView size of UIImageView changes. Here is the code
UIView *titleView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 30)];
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width/2, 30)];
title.text = _groupName;
[title setTextColor:[self colorFromHexString:#"#474747"]];
[title setFont:[UIFont fontWithName:#"Roboto-Bold" size:16]];
[title setTextAlignment:NSTextAlignmentCenter];
[title setBackgroundColor:[UIColor clearColor]];
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_imageView.layer.borderWidth = 0.1;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
title.frame = CGRectMake(title.frame.origin.x+20, title.frame.origin.y, self.view.frame.size.width/2, 30);
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(titleTapped:)];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:recognizer];
[titleView addSubview:title];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView addSubview:_imageView];
self.navigationItem.titleView = titleView;
Here are the images where you can see the changes
I check your code Add clips to bounds and do not need to title frame two times.
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.clipsToBounds = true;
Rather than setting the frame of the views directly, you might try using Auto Layout; I suspect what's happening is that your custom view is getting re-layed-out after the back button, and it's reverting to the image view's instrinsicSize.
What you could do instead is turn off translatesAutoresizingMaskIntoConstraints on the subviews, and then add constraints to position them, and finally add width and height constraints to the image view.
Using Auto Layout is also recommended if you ever intend to internationalize your app -- if you set up your constraints correctly, then the translated title will fit and you won't have to ever play around with hardcoded values.
when typing in the UITextView the text is not visible;
when typing is done the text is not visible.
How to make it visible?
The code is below. I do not put the UITextView related code intentionally as only font and alignment properties are set.
// Setup the background image view.
[self.backgroundImageView setBackgroundColor:[UIColor blackColor]];
[self.view sendSubviewToBack:self.backgroundImageView];
// Set the image view.
CGRect r = CGRectMake(0, 0, self.TextView.bounds.size.width, 455);
ImageView = [[UIImageView alloc] initWithFrame:r];
// Set up the scroll view for the image zooming in/out and scrolling.
imgScrollView = [[UIScrollView alloc] initWithFrame:ImageView.bounds];
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:ImageView];
[imgScrollView setBackgroundColor:[UIColor clearColor]];
[imgScrollView setContentSize:ImageView.frame.size];
imgScrollView.maximumZoomScale = 5.0;
imgScrollView.delegate = self;
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[imgScrollView addGestureRecognizer:doubleTapRecognizer];
[imgScrollView sendSubviewToBack:self.TextView];
[self.view addSubview:imgScrollView];
UPDATE
[imgScrollView addSubview:self.TextView];
the line of code above solved this.
I need to create a UIView with a UILabel subview programmatically because it works better than IB for animations. I cannot get the UIView to recognize the UILabel though. Here is my code from viewDidLoad:
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(764, 94, 200, 100)];
[label setText:#"Hello"];
[label setTextColor:[UIColor redColor]];
[label setBackgroundColor:[UIColor clearColor]];
UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(764, 94, 240, 198)];
[hintView setBackgroundColor:[UIColor colorWithRed:(34/255.f) green:(59/255.f) blue:(27/255.f) alpha:(255/255.f)]];
[hintView addSubview:label];
[self.view addSubview:hintView];
NSLog(#"subview %#", hintView.subviews);
Thanks
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(764, 94, 200, 100)];
You are setting wide frame try this
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
Change yourLable.frame.origin.x between (0) to (320 - withOfYourLabel)
Above code is same for your custom UIView (set X)
And if you want to get subViews of your custom view then follow
for(UIView *subView in hintViewsubviews)
{
/// here you got all subview of hintView
if([subView isKindOfClass:[UILabel class]])
{
// here you can also check that subView is UILabel or not ?
}
}
You are taking same frame size for both UIView and Label.bcz of that you are not getting proper output.See the changes which i have done to you code and try this
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(hintView.frame.origin.x+15, hintView.frame.origin.y+50, hintView.frame.size.width-60, hintView.frame.size.height-100)];
[label setText:#"Hello"];
[label setTextColor:[UIColor redColor]];
[label setBackgroundColor:[UIColor clearColor]];
UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(764, 94, 240, 198)];
[hintView setBackgroundColor:[UIColor colorWithRed:(34/255.f) green:(59/255.f) blue:(27/255.f) alpha:(255/255.f)]];
[hintView addSubview:label];
[self.view addSubview:hintView];
NSLog(#"subview %#", hintView.subviews);
The following snippet shows up on the screen on top of correct background, however text does not scroll.
What am i missing please?
-(void) viewWillAppear:(BOOL)animated {
UITextView *scrollableText = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, 300, 800)];
[scrollableText setOpaque:TRUE];
[scrollableText setBackgroundColor:[UIColor clearColor]];
[scrollableText setEditable:NO];
[scrollableText setText:#"some really really long text"];
[scrollableText setScrollEnabled:YES];
UIImage *backgroundImage = [[UIImage alloc] initWithCGImage:[UIImage imageNamed:#"about_bg.png"].CGImage];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundView addSubview:scrollableText];
[[self view] addSubview:backgroundView];
}
Try [textview setUserInteractionEnabled:YES] or textView.userInteractionEnabled = YES; in addition to setScrollEnabled.
i have the same problem and i resolve in this mode.
Put in your view a customView like OBGradientView and in this add your textView.
in the code add at view your ObGradientView and at it add textview.
work 100% trust to me
here the link for subview