I'm programming an application, in wich you create UIImageView-objects.
The color of these objects can be set.
But the next step I am trying to do is: set a TextView in the imageView, but this text should entered by the user (certainly realized with the class UIKeyboard.)
Im sum: I have a imageView, which needs an editable UITextView in it.
Do you have any idea how it could be realized?
Your approach is wrong.
You want an image view because the app lets you "create an image"
In reality and technically this isn't correct, what you will want to be doing is using a simple UIView to set he colour of an area, and you an place a UITextField or UITextView over that.
A UIImageView is a very specific UIView subclass for displaying images. You can't add your other UI inside of it, the best you could do is over it.
I you do use a UIView, when you're done you need to "convert" your UIView to an image which you can then use to save, send etc
For more about that check out Save UIView's representation to file
You simply want to subclass UIImageView and add a UITextView during init. Like this
#interface MyImageView : UIImageView {
}
#property (nonatomic, strong) UITextView *textView;
#end
And the implementation
#implementation MyImageView
- (id)init {
if ((self = [super init])) {
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(...)];
[self addSubview:self.textView];
}
return self;
}
#end
Related
In iOS 8 Apple introduced a new property under UIResponder called inputAccessoryViewController. However there has been no documentation explaining how one can use it.
Moreover, in iOS 8, setting the inputAccessoryView property of a UIViewController subclass and making the subclass the first responder seems to cause the view controller to leak.
My question is: How can I use an inputAccessoryViewController in a UIViewController subclass? Does that solve the memory leak problem?
If you want to use inputAccessoryViewController, you need to subclass any UITextView or UITextField controls and override the inputAccessoryViewController property readwrite.
#interface MyTextView: UITextView
#property (nonatomic, retain) UIInputViewController *inputAccessoryViewController;
#end
Note that the view controller you provide should be a UIInputViewController (which will give you access to input delegate and text document information) and will create the 'inputView' (as self.view) if you call [super loadView], which you can add your subviews to. This UIInputView will be styled appropriately for the keyboard. I haven't experimented with trying to load from .xib in this case.
I also found to properly size the inputView (and avoid a bunch of constraint errors), I added the following code
- (void)updateViewConstraints
{
self.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.superview.frame), myAccessoryHeight);
[super updateViewConstraints];
}
Add this method in your code. This will help you. from iOS 7 you need to return UIView only as accessoryView.
-(UIView *)inputAccessoryView{
UIView *accessoryView = [[UIView alloc] initWithFrame:toolView.frame];
toolView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
[accessoryView addSubview:toolView];
return accessoryView;
}
I have a UIScrollview.
In the UIScrollview there are 4 UIImageViews with images.
When I click on any UIImageview I want to know the name of that image.
UIImageView just keeps the pointer to the place the UIImage is stored inside the memory, so it's not possible with UIImageView. You would need to create a subclass of UIImageView with a NSString variable which stores the value to access it later. Something like this:
#import <UIKit/UIKit.h>
#interface MyImageView : UIImageView
#property (strong, nonatomic) NSString *imageName;
#end
The image names are not stored in UIImage objects so you have to keep them somewhere else for lookup. To detect touches in image views you either have to subclass them and add code for touch detection, add tap recognizers or replace them with buttons.
In my app, I have a list of tags corresponding to a user that I would like to display. The tag should have a text field and a button on the right hand side to delete the tag (much like the tags on this site when asking a question!)
I only want part of the tag to be clickable, so I don't want to place a UILabel inside a UIButton, and it seems that adding a UIButton to a UILabel is bad practice. What is the best way to create this object?
I want something similar to a UITableViewCell, but not a UITableViewCell because it won't be displayed in a table view.
Just to create a UIView subclass, we could name it TagView, give UILabel and UIButton to display, and don't forget to create a delegate protocol to for action response.
Maybe it like this.
#protocol TagViewDelegate
#optional
- (void)didSelectedTagView:(TagView *)tagView;
- (void)didTagViewAccessoryButtonTapped:(TagView *)tagView;
#end
#interface TagView : UIView
{
UILabel *label;
UIButton *accessoryButton;
}
#property (weak, nonatomic) id delegate;
#property (readonly, nonatomic) UILabel *label;
#end
Then implement a UITapGestureRecognizer for label and touchUpInside event for button
- (void)initUIElement {
// give it a gestureRecognizer for label to response the label did select
UITapGestureRecognizer *labelTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didSelectedLabel:)];
[label addGestureRecognizer:labelTapGR];
[accessoryButton addTarget:self action:#selector(accessoryTapped:) forControlEvents:UIControlEventTouchUpOutside];
}
in the method didSelectedLabel: and accessoryTapped: , give response to tagView's delegate like this.
- (void)didSelectedLabel:(UITapGestureRecognizer *)gestureRecognizer
{
[self.delegate didSelectedTagView:self];
}
- (void)accessoryTapped:(id)sender
{
[self.delegate didTagViewAccessoryButtonTapped:self];
}
There is a library you may take a look.
DWTagList
Just like cabellicar123 said, use a UIView and place your UILabel and UIButton inside it. You have a couple of ways to do this.
First, you can make a XIB file with the main view being your UIView, and just drag into it a UIButton and a UILabel and position them the way you want, and then in code load the nib when you need it.
Second is to create this UIView programmatically, and use the addSubview method and add your UILabel and UIButton that way. Make sure you have defined these two subviews with initWithFrame if you are using this method, so that they are correctly placed WITHIN the UIView. What I mean by this is that the origin of these views should be relative to the top left corner of the UIView.
I have a custom UITableViewCell and I want to style that using the UIAppearance protocol.
#interface MyCell : UITableViewCell<UIAppearance>
{
}
#property (nonatomic,weak) UIView *backgroundCellView UI_APPEARANCE_SELECTOR;
MyCell.m:
-(void) setBackgroundCellView:(UIView *)backgroundView
{
NSLog(#"setBackgroundCellView");
[super setBackgroundView:backgroundView];
}
In the app delegate I am doing the following:
[[MyCell appearance] setBackgroundCellView:[UIImageView .... with an image]];
It assign the background to few cells but not all the cells. Usually the assigned ones are in the middle.
One cannot assigning the same UIImageView to multiple cells and try to display that. The same UIView cannot be part of only one subview hierarchy.
What needs to be done instead is to make is a custom background view which has a UIImage configurable with UIAppearance.
Basically one should configure the background view's image using UIAppereance and not the background view itself.
this is what I designed in xib, and it's my expectation also.
My ViewControlle.h file:
#interface DetailHeadViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIScrollView *slideScrollView;
#property (weak, nonatomic) IBOutlet UIImageView *smallImageView;
#property (weak, nonatomic) IBOutlet UIImageView *barBGVImageView;
#end
My ViewController.m file (part of the source code)
- (void)viewDidLoad
{
[super viewDidLoad];
slideScrollView.pagingEnabled = YES;
slideScrollView.showsHorizontalScrollIndicator = NO;
slideScrollView.showsVerticalScrollIndicator = NO;
UIImageView * page1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headSection_hot1"]];
[slideScrollView addSubview:page1]; //Update 3
}
the result is this,
But actually the correct result is this,
Anyone can help to point out what the problem is?
Thanks in advance
update (remove bar image)
for easier, I remove bar image in xib and also comment out the relatived code.
Please see the following picture.
update 2
IB
To me it looks like your scroll view frame is not set correctly. It should probably be set to 0,0,480,320.
It also looks like the transparency isn't set on the BarImageView. Select the BarImageView in the xib, open up the inspector window, click on the 4th tab, set the background color to Clear Color and reduce the alpha.
You may also want to post how your adding the image to the scroll view as that could create issues as well.
Update 1:
In the viewDidLoad of your DetailHeadViewController try this:
self.scrollView.frame = self.view.bounds;
UIImageView * page1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headSection_hot1"]];
page1.frame = self.view.bounds;
[self.slideScrollView addSubview:page1];
self.scrollView.contentSize = self.view.bounds.size; // Note that the contentSize must be changed
// from this if you want to scroll.