How to move element programmatically in iOS? - ios

I have an element I have coded but I can't seem to get the code right so I can set the position of this on the screen. Here is my code:
- (void)setShowsRadar:(BOOL)showsRadar{
_showsRadar = showsRadar;
[_agController setShowsRadar:_showsRadar];
}
I want to position it like this:
initWithFrame:CGRectMake(5, 22, 60, 30)];

yourView.frame = CGRectMake(x,y,width,height);

Related

Text of textview is overlapping under textview

My question is simple and I think it is possible but I cant find it.
My design is like this
I give corner radius to textview and also set textContainerInset for padding from right and left both side
txtview_msg.textContainerInset = UIEdgeInsetsMake(5, 10, 10, 5)
Now my problem is like for first 2 line and last 2 line my text goes beneath textview so I need some solution for it.
Please help me into this. Thank you
try this code
UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, txtview_msg.frame.size.height - 30)];
txtview_msg.leftView = paddingView1;
txtview_msg.leftViewMode = UITextFieldViewModeAlways;
UIView *paddingView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, txtview_msg.frame.size.height - 30)];
txtview_msg.leftView = paddingView2;
txtview_msg.rightViewMode = UITextFieldViewModeAlways;
You can create at first a view of same size how much you given, put cornerRadius how much you given in textview now. than create textview viewSize - corner radius. place the textview exactly in middle of view. set the constraints from that view only, than it will work perfect how you want.
check this attached screenshot:

How do I keep adding UIButton and set them with different action programmatically?

Assuming I have a container that stores a list of items. By adding these items, I have to add a single UIView for each. I want to make a delete button that allows the user to delete the item that they don't want. How can I keep adding these buttons and separate them with different actions? Like this button is for deleting item A, and that button is for deleting item B?
P.S. This situation is not allow to use tableView, and I've already handled the view stacking part.
If you need me to show any of the code, please feel free to ask.
Updated:
The code of adding the Item:
-(void)appendAttachmentRow:(AttachmentItem *)attachment
{
AttachmentRowView * attachmentRowView = [[AttachmentRowView alloc]init];
screenWidth = CGRectGetWidth(self.view.bounds);
screenHeight = CGRectGetHeight(self.view.bounds);
// Set up the view in a single attachment row
// Attachment row container
CGRect attachmentRowFrame = CGRectMake(0, yLastLocation, screenWidth, 50);
UIView *attachmentRow = [[UIView alloc]initWithFrame:attachmentRowFrame];
// Attachment name label
CGRect attachmentNameLabelFrame = CGRectMake(70, 20, screenWidth / 3, 15);
UILabel *attachmentNameLabel = [[UILabel alloc]initWithFrame:attachmentNameLabelFrame];
// Attachment thumbnail image
CGRect attachmentImageThumbnailFrame = CGRectMake(10, 0, 50, 50);
UIImageView *attachmentImageThumbnail = [[UIImageView alloc]initWithFrame:attachmentImageThumbnailFrame];
CGRect attachmentRemoveFrame = CGRectMake(screenWidth - 40, 10, 30, 30);
attachment.attachmentRemove = [[UIButton alloc]initWithFrame:attachmentRemoveFrame];
[attachment.attachmentRemove setImage:[UIImage imageNamed:#"removeAttachmentButton"] forState:UIControlStateNormal];
[attachment.attachmentRemove addTarget:self action:#selector(removeAttachment:) forControlEvents:UIControlStateNormal];
attachmentImageThumbnail.image = attachment.attachmentImage;
attachmentNameLabel.text = attachment.attachmentName;
attachmentRow.layer.borderColor = [UIColor lightGrayColor].CGColor;
attachmentRow.layer.borderWidth = 1.0f;
[attachmentRow addSubview: attachmentImageThumbnail];
[attachmentRow addSubview: attachmentNameLabel];
[attachmentRow addSubview: attachment.attachmentRemove];
[[self attachmentCellCellIt] addSubview: attachmentRow];
[attachmentArray addObject:attachment];
yLastLocation += 50;
[[self attachmentCellCellIt]setFrame:CGRectMake(0, 337, screenWidth, yLastLocation)];
You need to give tag to button after creating UIView for an attachment.
Keep the method name same and try to work with the tag value.
For ex :
button.tag = 1000; // while creating it.
In method you passed UIButton as parameter
Inside method body
NSInteger tag = button.tag
[array removeObjectAtIndex:tag];
Hope i understand your situation,
You want to set selector dynamically. Say you have following selector declarations.
-(void)onPressA:(id)sender{ ... }
-(void)onPressB:(id)sender{ ... }
-(void)onPressC:(id)sender{ ... }
-(void)onPressD:(id)sender{ ... }
Now need to take an NSArray or other storage to store them. Let save them in array. To do that, you need to convert them into NSString as following
NSArray *selectorArr = #[NSStringFromSelector(#selector(onPressA:)),
NSStringFromSelector(#selector(onPressB:)),
NSStringFromSelector(#selector(onPressC:)),
NSStringFromSelector(#selector(onPressD:))];
Now you can back & forth from NSString to SEL & SEL to NSString as following.
SEL selector = NSSelectorFromString(selectorArray[/*suitable index*/]);
Now you can easily add and remove target by using
[btn addTarget:/*target*/ action:/*selector*/ forControlEvents:/*UIControlEvents*/];
[btn removeTarget:/*target*/ action:/*selector*/ forControlEvents:/*UIControlEvents*/];
addTarget:action:forControlEvents: appledoc
removeTarget:action:forControlEvents: appledoc
You need to keep track which SEL was previously assigned so that you can remove it.
Happy coding :)
why you want to set different actions? you can do this by set tag property of button. on action you can check tag value and can do different tasks i guess.
You can get the button object from which the function is called in the following method declaration in "sender" object.
-(IBAction)buttonClicked:(id)sender
from this you can get the tag of the UIButton from which the function is called and perform the desired action.

Modified navigationbar causes issues on button item

In our project we wanted to change the size of navigationbar. Have achieved it with
#implementation UINavigationBar (customNav)
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(self.superview.bounds.size.width, 55.0f);
}
#end
But the problem here is.. as you would see go button is little down from screen edge. But I wanted it to be in ultimate corner of screen. I use backgroundColor:, setTitle:forState:methods for constructing look of go button. also back button to be vertically middle. How can I achieve that. Tried Using edgeInsets: but no hope.
Edit: Go button height is 10px less than barheight.
if I give equal height. Below is the result
in somewhat I have tried your coding.Check that
UIButton *goBtn = [UIButton buttonWithType:UIButtonTypeCustom];
goBtn.frame = CGRectMake(0, 0, 63, 80);
goBtn.backgroundColor = [UIColor redColor];
UIView *goButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 50)];
NSLog(#"goButtonView bounds are - %#", NSStringFromCGRect([goButtonView bounds]));
goButtonView.bounds = CGRectOffset(goButtonView.bounds, -17, 0);
NSLog(#"goButtonView bounds are - %#", NSStringFromCGRect([goButtonView bounds]));
[goButtonView addSubview:goBtn];
UIBarButtonItem *goButton = [[UIBarButtonItem alloc] initWithCustomView:goButtonView];
self.navigationItem.rightBarButtonItem = goButton;
Below the references
Issue with button on the left side of the uinavigation bar
UINavigationBar UIBarButtonItems much larger click area than required

Can't centre UITextField in UIView

I'm trying to add my UITextField to a UIView that is placed in the centre of the table. The UIView works fine and is positioned correctly. however the UITextField is in the wrong position and at the bottom left of the screen. Code below:
self.addFriendView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
self.addFriendView.center=self.view.center;
[self.addFriendView setBackgroundColor:[UIColor whiteColor]];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 40)];
nameField.delegate=self;
nameField.center=self.view.center;
nameField.placeholder=#"enter username";
[self.addFriendView addSubview:nameField];
[self.tableView.superview addSubview:self.addFriendView];
When I am placing the UITextField in the centre of the UIView do the coordinates need to be inside the UIView's coordinates or the frames?
That's because addFriendView.center is it's center in it's superview coordinate which is {150, 25}. What you want is that put the center of nameField in the center of addFriendView in addFriendView's coordinate.
So, use this:
nameField.center = CGPointMake(CGRectGetMidX(addFriendView.bounds),
CGRectGetMidY(addFriendView.bounds));
Updated to use CGRectGetMidX and CGRectGetMidY instead.
Because you have added nameField as a subview of self.addFriendView, its center will need to be relative to self.addFriendView.
nameField.center=self.view.center;
This line here is causing your issue. If fact, it's also an issue where you assign self.addFriendView's center, but by luck, self.view's bounds happen to be the same as its superview's.
Instead, you'll want to do this:
nameField.center=CGPointMake(CGRectGetMidX(self.addFriendView.bounds),
CGRectGetMidY(self.addFriendView.bounds));
and also, just for robustness:
self.addFriendView.center=CGPointMake(CGRectGetMidX(self.view.bounds),
CGRectGetMidY(self.view.bounds));

iOS: UIButton doesn't show up

I'm creating a simple button and adding it as a subview to main view. It doesn't show up though.
This is the code for setting it up:
UIButton* contactButton = [[UIButton alloc] init];
contactButton.titleLabel.text = #"Contact Developer";
[contactButton sizeToFit];
[self.view addSubview:contactButton];
NSLog(#"X: %f || Y: %f || Width: %f || Height: %f", contactButton.frame.origin.x, contactButton.frame.origin.y, contactButton.frame.size.width, contactButton.frame.size.height);
As you may have noticed, I placed a bit of debug code at the end to see the position and dimensions of the button. They are: X: 0, Y: 0, Width: 30, Height: 34
This means it should be showing up in the upper left corner but it doesn't. Whats wrong?
One possible reason for this is that you used titleLabeldirectly. Consider using setTitle:forState: method instead.
To be sure, consider setting backgroundColor, as a debug step, to make sure it's appearing.
Edit As others suggested, try using buttonWithType: instead of [[UIButton alloc] init]. I suggest using UIButtonTypeSystem instead of UIButtonTypeCustom.
You should initialise the button using the following:
UIButton* contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
Try constructing your button a different way:
UIButton* contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
contactButton.backgroundColor = [UIColor redColor];
contactButton.titleLabel.text = #"Contact Developer";
[contactButton sizeToFit];
[self.view addSubview:contactButton];
Are you sure that the button it's not being shown?
I think you are setting the title of the button incorrectly and because of the fact that a default UIButton has clear background and white title, it´s not visible if your superview is white too.
Instead of setting:
contactButton.titleLabel.text = #"Contact Developer"
use:
[contactButton setTitle:#"Contact Developer" forState:UIControlStateNormal]
But you can first try to set a backgroundColor to the button, different that the superview, to see if it's added.
Hope it helps!
(Sorry for my english if i have a mistake)

Resources