I've got a UIButton declared in interface builder with an outlet set as follows:
#property (weak, nonatomic) IBOutlet UIButton *loginButton;
It works fine until I insert a subview. Then it stops responding. Here's the offending line:
[self.loginButton insertSubview:_gradientButtonView atIndex:0];
Commenting that out makes the button work again. Here's all the code involved (using the awesome SSToolkit and a custom category on UIColor):
SSGradientView *_gradientButtonView = [[SSGradientView alloc] initWithFrame:self.loginButton.layer.bounds];
_gradientButtonView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_gradientButtonView.topBorderColor = [UIColor colorWithRed:0.558f green:0.599f blue:0.643f alpha:1.0f];
_gradientButtonView.topInsetColor = [UIColor colorWithWhite:1.0f alpha:0.3f];
_gradientButtonView.colors = [NSArray arrayWithObjects:[UIColor from_hex:#"E6E7E8"], [UIColor from_hex:#"A7A9AC"], nil];
_gradientButtonView.layer.cornerRadius = 5;
self.loginButton.layer.cornerRadius = 5;
self.loginButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
[self.loginButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.loginButton.layer.masksToBounds = YES;
_gradientView.bottomBorderColor = [UIColor colorWithRed:0.428f green:0.479f blue:0.520f alpha:1.0f];
_gradientButtonView.clipsToBounds = YES;
_gradientButtonView.userInteractionEnabled = YES;
[self.loginButton insertSubview:_gradientButtonView atIndex:0];
self.loginButton.userInteractionEnabled = YES;
This was answered in the comments above:
Is there a specific reason you're writing
_gradientButtonView.userInteractionEnabled = YES;? To my knowledge, this prevents the view from sending touch events up the responder
chain
Related
hi i'm begginer in objective-c and i want to learn how i can make UILabel in center of screen
this is my code:
XXRootViewController.h
#interface XXRootViewController : UIViewController{
UILabel *title; } #end
XXRootViewController.m
#import "XXRootViewController.h"
#implementation XXRootViewController {
NSMutableArray *_objects;
}
- (void)loadView {
[super loadView];
self.view = [[[UIView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]]
autorelease];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
title.text = #"This is Sasuke's first app :)";
[self.view addSubview:title];
}
#end
Try this code i make some changes in you code
self.view = [[UIView alloc]
initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width - 50,200)];
title.text = #"This is Sasuke's first app :)";
title.textAlignment = NSTextAlignmentCenter;
title.center = self.view.center;
[self.view addSubview:title];
You just need to set frame in center of the screen ...
change your one line code with it -
title = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 200,self.view.frame.size.height/2 - 100,400,200)];
now your label is shown in the center of screen .. This will help you change the frame according to your use...
Has Hardik Thakkar said, the best way would be to use the center method. That way, even if your label would grow, it will always be centred.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
label.text = #"something";
label.center = self.view.center; (this should always be you superview)
Now, even if your label is bigger than the size you were expecting, it will always be centred
I'm a noob in Objective-C and I have one question.
I have one UILabel object that I adding to one UIView with this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,10,self.view.frame.size.width-15-70, 30)];
label.tag = 1;
label.font = [PublicObject fontTexts:17];
label.textAlignment = NSTextAlignmentRight;
label.textColor = [UIColor whiteColor];
[cell setBackgroundColor:[UIColor clearColor]];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
view.backgroundColor = [PublicObject colorWithHexString:#"cd4110"];
label.text = [filterData objectAtIndex:indexPath.row];
view addSubview:label];
Now I want get one subview in my view where this subview has tag = 1 and save it on another object like this:
UILabel *tagLabel;
tagLabel = // I want get one subview in view where tag = 1
Please help me understand how to do this.
Example with UILabel:
UILabel *label = (UILabel *)[self.view viewWithTag:1];
good luck!
You can get your subviews with for loop iteration
for (UIView *i in self.view.subviews){
if([i isKindOfClass:[UILabel class]]){
UILabel *newLbl = (UILabel *)i;
if(newLbl.tag == 1){
/// Write your code
}
}
}
Swift
let label:UILabel = self.view.viewWithTag(1) as! UILabel
You can get the subview with the code which others have mentioned, just like
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
But an important point to remember,
Make sure the parent view doesn't have the same tag value as of the subview. Otherwise the viewWithTag: method will return the receiver view (on which you are calling viewWithTag:) instead of returning the actual subview you want.
So keep the parent view and child views tags distinct whenever you need to use viewWithTag:.
Swift 3, Swift 4 and Swift 5
if let subLabel: UILabel = primaryView.viewWithTag(123) as? UILabel {
subLabel.text = "do things here :-D"
}
You could use the viewWithTag: method.
If you are on the same view
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
Also, if you want a new instance of UILabel
UILabel *newTagLabel = [tagLabel copy];
//customize new label here...
[view addSubView:newTagLabel];
I am programatically creating a UITextField, adding it to view and then start editing by calling
[textField becomeFirstResponder];
But the issue is this call triggers call to textFieldDidEndEditing: delegate method. The stack trace points to [textField _resignFirstResponder] which is invoked by becomeFirstResponder. This is happening on simulator and iOS 7. How do I avoid this ? This is causing lot of issues as I don't want textFieldDidEndEditing: to be called without keyboard getting dismissed.
EDIT : Here is how I create UITextField.
UITextField *titleField = [[[UITextField alloc] init] autorelease];
[titleField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
titleField.textColor = [UIColor colorWithWhite:1.f alpha:0.8];
titleField.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:40.f];
titleField.textAlignment = NSTextAlignmentCenter;
titleField.borderStyle = UITextBorderStyleBezel;
titleField.autocorrectionType = UITextAutocorrectionTypeNo;
// titleField.borderStyle = UITextBorderStyleLine;
titleField.frame = CGRectMake(0, 0, 200, 80);
titleField.center = self.previewView.center;
titleField.delegate = self;
NSDictionary *titleDictionary = [self titleDictionary];
if (titleDictionary) {
titleField.text = [titleDictionary objectForKey:kTitleStringKey];
NSString *fontName = [titleDictionary objectForKey:kTitleFontNameKey];
CGFloat fontSize = [[titleDictionary objectForKey:kTitleFontSizeKey] floatValue];
titleField.font = [UIFont fontWithName:fontName size:fontSize];
NSData *textColorData = (NSData *)[titleDictionary objectForKey:kTitleTextColorKey];
titleField.textColor = [NSKeyedUnarchiver unarchiveObjectWithData:textColorData];
CGSize size = [titleField.text sizeWithFont:titleField.font];
titleField.frame = CGRectMake(0, 0, size.width + 40, 80);
titleField.center = self.previewView.center;
}
[self.view addSubview:titleField];
[titleField becomeFirstResponder];
Ok I found it. I had a UILongPressGestureRecognizer that triggered adding UITextField routine, and that is incidentally called twice on long press (need to figure out why, but its a separate issue). So becomeFirstResponder was called twice.
after going through the questions related to UINavigationController item i came to post my exact problem ..
firstly i am not add a UINavigationController in MainWindow.xib i created and adding UINavigationController in AppDelegate file
after that in a UIViewController (not in rootViewController) class I have to add a rightbarbutton of Done type I am
adding it in a searchbarDelegate Method showing as bold:-
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
//This method is called again when the user clicks back from teh detail view.
//So the overlay is displayed on the results, which is something we do not want to happen.
if(exhibDataObj.searching)
return;
//Add the overlay view.
if(ovController == nil)
ovController = [[OverlayViewController alloc] initWithNibName:#"OverlayView" bundle:[NSBundle mainBundle]];
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
//Parameters x = origion on x-axis, y = origon on y-axis.
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;
ovController.rvController = self;
[self.theTableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
exhibDataObj.searching = true;
letUserSelectRow = NO;
self.theTableView.scrollEnabled = NO;
//Add the done button.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(doneSearching_Clicked:)];
}
but this never works .
Any help will be more appreciable.
I've tested your code real quick and it seems to be working fine for me, however, I've had the same problem once back then it turned out I was setting the rightBarButtonItem to nil in my viewDidLoad method after setting it to a button. you should make sure your not making the same mistake somehow.
However if that does not work for you you can always try setting a custom button to it:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];
button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"done.png"]];
[button addTarget:self action:#selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;
I have created the following subclass to do some custom drawing:
// DocumentIconView.h
#interface DocumentIconView : UIView
{
UIImageView *documentIconView;
CloseHandle *closeHandle;
UILabel *filenameLabel;
}
#property (nonatomic, strong) UIImageView *documentIconView;
#property (nonatomic, strong) CloseHandle *closeHandle;
#property (nonatomic, strong) UILabel *filenameLabel;
+ (DocumentIconView *)documentIconWithFrame:(CGRect)viewFrame
previewImage:(UIImage *)previewImage
title:(NSString *)title;
This works well for the most part (I can instantiate objects, and do custom drawing), however, I cannot access some of its properties from another classes.
DocumentIconView *iconView = [DocumentIconView documentIconWithFrame:frame
previewImage:[UIImage imageNamed:#"GenericDocumentIcon.png"]
title:[NSString stringWithFormat:#"test"]];
iconView.backgroundColor = [UIColor lightGrayColor]; // this works
iconView.filenameLabel.backgroundColor = [UIColor darkGrayColor]; // this does not work - no error message
[documentsView addSubview:iconView];
I can read and write first-level properties, but cannot drill deeper; when trying to read properties, the value returned is (null)
I am fairly new at subclassing, so I think I am missing something really obvious here. Any help would be greatly appreciated.
EDIT: the method for instantiating the view in question:
+ (DocumentIconView *)documentIconWithFrame:(CGRect)viewFrame
previewImage:(UIImage *)previewImage
title:(NSString *)title
{
DocumentIconView *view = [[DocumentIconView alloc] initWithFrame:viewFrame];
// Close handle's size is assigned here
CGSize closeHandleSize = CGSizeMake(27, 27);
// The document preview image's frame is calculated by shrinking it by the close handle's size
CGRect documentPreviewFrame = CGRectMake(closeHandleSize.width / 2,
closeHandleSize.height / 2,
viewFrame.size.width - closeHandleSize.width,
viewFrame.size.height - closeHandleSize.height - 20); // 20 points is the filenameLabel's height
UIImageView *documentPreviewView = [[UIImageView alloc] initWithFrame:documentPreviewFrame];
documentPreviewView.contentMode = UIViewContentModeScaleAspectFit;
documentPreviewView.backgroundColor = [UIColor clearColor];
documentPreviewView.image = previewImage;
[view addSubview:documentPreviewView];
CGRect closeHandleFrame = CGRectMake(0, 0, closeHandleSize.width, closeHandleSize.height);
CloseHandle *closeHandleView = [[CloseHandle alloc] initWithFrame:closeHandleFrame];
closeHandleView.alpha = 0.0;
closeHandleView.tag = kCloseHandleTag;
[view addSubview:closeHandleView];
CGRect filenameFrame = CGRectMake(0,
viewFrame.size.height - 20,
viewFrame.size.width,
20);
UILabel *filenameLabel = [[UILabel alloc] initWithFrame:filenameFrame];
filenameLabel.backgroundColor = [UIColor clearColor];
filenameLabel.text = title;
filenameLabel.font = [UIFont boldSystemFontOfSize:17];
filenameLabel.textColor = [UIColor whiteColor];
filenameLabel.textAlignment = UITextAlignmentCenter;
[view addSubview:filenameLabel];
view.tag = kDocumentIconTag;
return view;
}
In your documentIconWithFrame:... method you're using a local variable (filenameLabel) that you're adding to the view. That means your instance variable is never instantiated and is always nil.
Just change this:
UILabel *filenameLabel = [[UILabel alloc] initWithFrame:filenameFrame];
to this:
filenameLabel = [[UILabel alloc] initWithFrame:filenameFrame];
and the same for the other instance variables.