rightView property error in iOS7 for UITextField - ios

I am setting an imageView as the rightView of a textfield. This is my code:
UITextField *textField = [[UITextField alloc]initWithFrame:controlFrame];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setBackgroundColor:[UIColor whiteColor]];
CGRect imageViewFrame = CGRectMake(controlFrame.origin.x + controlFrame.size.width - 20,controlFrame.origin.y, 15.0,controlFrame.size.height-10);
UIImage *image = [UIImage imageNamed:#"arrow.png"];;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setFrame:imageViewFrame];
[textField setRightViewMode:UITextFieldViewModeAlways];
textField.rightView = imageView;
Its working perfect on iOS 6.1 devices
But in iOS 7 devices
Any Solution for this?
Thanks in advance

Was just working on this myself and used this solution:
- (CGRect) rightViewRectForBounds:(CGRect)bounds {
CGRect textRect = [super rightViewRectForBounds:bounds];
textRect.origin.x -= 10;
return textRect;
}
This will move the image over from the right by 10 instead of having the image squeezed up against the edge in iOS 7.
Additionally, this was in a subclass of UITextField, which can be created by:
Create a new file that's a subclass of UITextField instead of the default NSObject
Add a new method named - (id)initWithCoder:(NSCoder*)coder to set the image
- (id)initWithCoder:(NSCoder*)coder {
self = [super initWithCoder:coder];
if (self) {
self.clipsToBounds = YES;
[self setRightViewMode:UITextFieldViewModeUnlessEditing];
self.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"textfield_edit_icon.png"]];
}
return self;
}
You may have to import #import
Add the rightViewRectForBounds method above
In Interface Builder, click on the TextField you would like to subclass and change the class attribute to the name of this new subclass

There's another faster option, no need to subclass your uitextfield. There's an example
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 6)];
[imageView setImage:[UIImage imageNamed:#"grey_filter.png"]];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 6)];
[paddingView addSubview:imageView];
Just create a View bigger than your image with your padding. Add your UIImageView and then put it as left/right view in your textfield.

Related

iOS Calc X position to place elements on a View

I want to add a Label and an Image in titleView of my NavigationItem. I am adding UILabel and UIImageView to a UIView and setting it as titleView of the navigation Item. Things are being added, but I am not able to calculate the length of label and place image next to it.
My code is :-
// Title Name + Image
UIView *titView = [[UIView alloc] initWithFrame:self.navigationItem.titleView.bounds];
self.navigationItem.titleView = titView;
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(-10, -20, 150, 30)];
title.text = #"Bunny ";
[title setTextColor:[UIColor orangeColor]];
[titView addSubview:title];
float x2 = 30; //+ title.bounds.size.width;
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"return" ]];
img.frame = CGRectMake(x2, -20, img.bounds.size.width, 30);
[titView addSubview:img];
I am looking for some way to calc the width text and set accordingly the width of the label. Right now I have set the width of the label as 150. And secondly, calc the x position to place the image just next to the label.
Tried many ways, but nothing works as expected. How can I achieve this ? Can you he give some guidelines such that regardless of length of text of label, things works as expected and UIView gets placed in the center of navigation item.
Any help is highly appreciated. Thanks.
You can call
[title sizeToFit];
after setting its properties and it will size itself to match the contained string.
Set the imageViews x origin to
CGRectGetMaxX(title.frame) + desiredSpacing;
And it could help to subclass UIView to achieve the final results by overwriting layoutSubviews and placing your views there, since the titleView's frame can be adjusted by the navigationBar...basically like this:
#implementation TitleView{
UILabel *_titleLabel;
UIImageView *_img;
}
- (id)initWithTitle:(NSString *)title andImage:(UIImage *)image
{
self = [super init];
if (self) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = title;
[_titleLabel setTextColor:[UIColor orangeColor]];
[_titleLabel sizeToFit];
[self addSubview:_titleLabel];
_img = [[UIImageView alloc] initWithImage:image];
[self addSubview:_img];
}
return self;
}
- (void)layoutSubviews{
CGFloat spacingBetweenTextAndImage = 0;
CGFloat width = CGRectGetWidth(_titleLabel.frame)+CGRectGetWidth(_img.frame)+spacingBetweenTextAndImage;
CGFloat x = (CGRectGetWidth(self.bounds)-width)/2;
_titleLabel.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_titleLabel.bounds))/2, CGRectGetWidth(_titleLabel.bounds), CGRectGetHeight(_titleLabel.bounds));
x+=CGRectGetWidth(_titleLabel.bounds)+spacingBetweenTextAndImage;
_img.frame = CGRectMake(x, (CGRectGetHeight(self.bounds)-CGRectGetHeight(_img.bounds))/2, CGRectGetWidth(_img.bounds), CGRectGetHeight(_img.bounds));
}
#end
use in your viewController:
UIView *titleView = [[TitleView alloc] initWithTitle:#"Bunny" andImage:[UIImage imageNamed:#"return" ]];
self.navigationItem.titleView = titleView;

how do you center a custom UINavigation Bar Image

So I have an navigation bar I would like to customize and center no matter what I do I either get the image tiling like so.
Here is the code i am using to set the self.navigation.titleView in my UITableControllerView
self.navigationItem.titleView = [self titleView]; // happens in viewDidLoad
- (UIView *)titleView{
UIImage *headerImage = [UIImage imageNamed:#"header#2x"];
UIImageView *containerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headerImage.size.width, headerImage.size.height)];
return containerView;
}
Thanks.
UIImageView *imgtitle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
[imgtitle setContentMode:UIViewContentModeCenter];
self.navigationItem.titleView = imgtitle;
imgtitle.image = [UIImage imageNamed:#"logo.png"];

Subview of custom UIButton class doesn't trigger click

I'm writing a custom UIButton subclass, to create my own fully customized button
to have total controll. Still, I'm having problems when I try to add a subview
to that UIButton's view. The subview blocks the "touch" events so it won't bubble to
the UIButton itself..
This is a demonstration:
I first create my CustomNavigationButton, with a frame.. It's magenta.
I can see the magenta on the screen so it's there. Secondly, I add a subview
to that CustomNavigationButton (which is green), I can see the green so it's
there, but if I click the green rectangle (subview), the "UIControlEventTouchUpInside"
doesn't get called on my CustomNavigationButton..
In my AppDelete:
CustomNavigationButton* mapBtn = [[CustomNavigationButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
mapBtn.backgroundColor = [UIColor magentaColor];
[mapBtn addTarget:self action:#selector(goMapHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapBtn];
And here's my CustomNavigationButton class (which is a subclass of UIButton)
#implementation CustomNavigationButton
#synthesize bg;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// STORE INITIAL FRAME
self.initialFrame = frame;
bg = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 40, 40)];
bg.backgroundColor = [UIColor greenColor];
[bg setUserInteractionEnabled:YES];
[self addSubview:bg];
}
return self;
}
#end
I figured out how to do it!
If "bg" is the subview I'm adding to the UIButton, then you should do:
bg.userInteractionEnabled = NO;
bg.exclusiveTouch = NO;
But keep in mind that, if your subview extends the frame of the UIButton,
a touch will not occur! You can check if your subview exceeds the UIButton
by giving the UIButton a background-color.

UITextView does not scroll, assistance needed

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

How to create a very custom uibutton like UITableViewCellStyleSubtitle in iOS

I would like to create a button with an image, title and description similar to the UITableViewCellStyleSubtitle. I would like to do this programmatically.
Does anyone know how I can accomplish this?
You could create a category of the UIButton class. Here's a barebones setup for you:
In the .h file:
#interface UIButton (YourCategoryName)
- (void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description;
#end
In the .m file:
#implementation UIButton (YourCategoryName)
- void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description {
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 50)]];
[self addSubview:titleLabel];
[titleLabel release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image];
[self addSubview:imageView];
[imageView release];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 100)];
[self addSubview:descriptionLabel];
[descriptionLabel release];
}
In the above code, you can adjust the frames as needed. Once this has been setup, just create your button like normal in your view/viewcontroller and call the method above:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 300, 200);
[myButton setupButton:myTitle image:myImage description:myDesc];
Subclass UIControl and override drawRect and/or add subviews to create the desired appearance.

Resources