How to setting NavigationBar's barButtonItems - ios

Now,I found the problem maybe from my custom button--DJExchangeImageTitleButton.This is the code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
frame.size.width += DJMargin;
[super setFrame:frame];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 如果仅仅是调整按钮内部titleLabel和imageView的位置,那么在layoutSubviews中单独设置位置即可
// 1.计算titleLabel的frame
self.titleLabel.x = self.imageView.x;
// 2.计算imageView的frame
self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + DJMargin;
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];
// 只要修改了文字,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[super setImage:image forState:state];
// 只要修改了图片,就让按钮重新计算自己的尺寸
[self sizeToFit];
}
-(void)setHighlighted:(BOOL)highlighted{};
Who can tell me what's wrong with my customed button?I will appreciate a lot.
When I set navigationBar's left,right and titleView,and it's normal;
However,every times,I push to another controller,and I pop back,the left and right barButtonItems become longer and longer,the titleView is shorter and shorter.
This is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupNaviBar];}
-(void)setupNaviBar{
//leftButton
DJExchangeImageTitleButton *titleBtn = [[DJExchangeImageTitleButton alloc]init];
[titleBtn setImage:[UIImage imageNamed:#"homgpage_navi_left"] forState:UIControlStateNormal];
[titleBtn setTitle:#"深圳" forState:UIControlStateNormal];
titleBtn.backgroundColor = [UIColor redColor];
titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
titleBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:titleBtn];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = #[negativeSpacer,leftItem];
//searchBar
UIButton *btn = [[UIButton alloc]init];
btn.backgroundColor = [UIColor whiteColor];
btn.frame = CGRectMake(0, 0, 250, 25);
btn.layer.cornerRadius = 3;
[btn setImage:[UIImage imageNamed:#"search_icon"] forState:UIControlStateNormal];
[btn setTitle:#"请输入要搜索的职位名称" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.imageEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
[btn addTarget:self action:#selector(goSearch) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = btn;
//rightButton
DJExchangeImageTitleButton *notiBtn = [[DJExchangeImageTitleButton alloc]init];
notiBtn.backgroundColor = [UIColor redColor];
[notiBtn setTitle:#"通知" forState:UIControlStateNormal];
[notiBtn setImage:[UIImage imageNamed:#"homgpage_navi_right"] forState:UIControlStateNormal];
notiBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
notiBtn.frame = CGRectMake(0, 0, 13, 25);
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:notiBtn];
self.navigationItem.rightBarButtonItem = right;
}
How can I solve this problem?Sorry I can't post a image to show my question.

Related

UINavigationController centered button using obj-c

I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}

App crashes with received memory Exception in ARC while scrolling the scrollview with out using images

I am Using UIScrollview and adding subView to display the message. While scrolling it gives received memory exception. I am struggling to find this from 15 days. Please help me to solve this.
To display I am loading 5 messages first time. After scrolling adding the subView according to array count.If it is subView is increased according the message length.If it is long messages,It crashes with received memory exception.
This is my code
-(void)createView:(float)yAxis
{
// NSLog(#"Create view %f",yAxis);
for (UIView * view in backgroundScroll.subviews) {
if (view.tag == 15) {
[view removeFromSuperview];
}
}
for(int i=0;i<[pageMessageArray count];i++)
{
UIView *backgrounf_View=[self messageChildElements:yAxis :i];
[del.msglistViewArray addObject:backgrounf_View];
UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i];
[backgroundScroll addSubview:backgrounf_View1];
yAxis=yAxis+oldY_Axis+65;
}
CGRect contentRect = CGRectZero;
for (UIView *view in backgroundScroll.subviews)
{
contentRect = CGRectUnion(contentRect, view.frame);
}
//UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i];
backgroundScroll.contentSize = CGSizeMake(backgroundScroll.frame.size.width, yAxis-10);
lblPermenentmessage.frame=CGRectMake(30, contentRect.size.height+20,800, 50);
//LastMessageYaxis=contentRect.size.height+20;
lblPermenentmessage.text=#"Messages are displayed for 24-48 hours and automatically deleted permanently.";
}
-(UIView *)messageChildElements:(float)yAxis :(int)index
{
// NSLog(#"Create view %f",yAxis);
communityObject *objMessage=[del.messageArray objectAtIndex:index];
UIView *backgrounf_View=nil;
UITextView *txt_Message=nil;
UIButton *btnURL =nil;
UIButton *btnAttachment=nil;
UIButton *btnDate=nil;
UIButton *btnSendBy=nil;
UIButton *btnAttachmentName=nil;
UIButton *btnSendTo=nil;
NSString *labelText = objMessage.Message;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:8];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
CGFloat height=[self getNewsize:objMessage.Message :backgroundScroll.frame.size.width-30];
backgrounf_View=[[UIView alloc]initWithFrame:CGRectMake(26,yAxis,backgroundScroll.frame.size.width-30,height)];
if([objMessage.inorout isEqualToString:#"out"])
{
//ye
[backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:247/255.0f blue:193/255.0f alpha:1.0f]];
[[backgrounf_View layer] setBorderWidth:1.0f];
[[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:236/255.0f green:237/255.0f blue:178/255.0f alpha:1.0f].CGColor];
}
else
{
//[cell.lblName addTarget:self action:#selector(replayClick:) forControlEvents:UIControlEventTouchUpInside];
[backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:215/255.0f blue:245/255.0f alpha:1.0f]];
[[backgrounf_View layer] setBorderWidth:1.0f];
[[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:226/255.0f green:188/255.0f blue:218/255.0f alpha:1.0f].CGColor];
}
// backgrounf_View.autoresizingMask=UIViewAutoresizingFlexibleWidth;
backgrounf_View.layer.cornerRadius=10;
//backgrounf_View.tag=15;
backgrounf_View.clipsToBounds=YES;
backgrounf_View.tag=objMessage.indexValue;
txt_Message = [[UITextView alloc] initWithFrame:CGRectMake(12, 6, backgroundScroll.frame.size.width-40, height)];
txt_Message.backgroundColor=[UIColor redColor];
[txt_Message setScrollEnabled:NO];
[txt_Message setUserInteractionEnabled:NO];
[txt_Message setBackgroundColor:[UIColor clearColor]];
//txt_Message.autoresizingMask=UIViewAutoresizingFlexibleWidth;
txt_Message.attributedText=attributedString;
//[txt_Message sizeToFit];
if([del.device isEqualToString:#"iphone"])
{
[txt_Message setFont:[UIFont fontWithName:#"Arial" size:17]];
}
else
{
[txt_Message setFont:[UIFont fontWithName:#"Arial" size:21]];
}
[backgrounf_View addSubview:txt_Message];
oldY_Axis=height+20;
if (objMessage.Link.length>0)
{
btnURL = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnURL.frame = CGRectMake(11,height,backgroundScroll.frame.size.width-100,40);
[btnURL setTitle:objMessage.Link forState:UIControlStateNormal];
btnURL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnURL.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
// btnURL.backgroundColor=[UIColor redColor];
btnURL.tag=objMessage.indexValue;
btnURL.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnURL.titleLabel.numberOfLines=2;
btnURL.titleLabel.font=[UIFont fontWithName:#"Arial" size:20];
btnURL.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnURL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnURL];
oldY_Axis=height+btnURL.frame.size.height+20;
}
if (objMessage.Filename.length>0)
{
btnAttachment= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAttachment.frame = CGRectMake(11, oldY_Axis,40, 40);
btnAttachment.tag=objMessage.indexValue;
UIImage *buttonattachment=[UIImage imageNamed:#"attach.png"];
[btnAttachment setBackgroundImage:buttonattachment forState:UIControlStateNormal];
btnAttachment.userInteractionEnabled=YES;
btnAttachmentName = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAttachmentName.frame = CGRectMake(47,oldY_Axis+5,backgroundScroll.frame.size.width-150,60);
[btnAttachmentName setTitle:objMessage.Filename forState:UIControlStateNormal];
btnAttachmentName.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnAttachmentName.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnAttachmentName.tag=objMessage.indexValue;
btnAttachmentName.titleLabel.font=[UIFont fontWithName:#"Arial" size:20];
btnAttachmentName.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnAttachmentName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnAttachment];
[backgrounf_View addSubview:btnAttachmentName];
oldY_Axis=oldY_Axis+btnAttachment.frame.size.height+30;
}
btnDate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnDate.frame = CGRectMake(11,oldY_Axis,130,30);
[btnDate setTitle:[NSString stringWithFormat:#"%#:",objMessage.Date] forState:UIControlStateNormal];
btnDate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnDate.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnDate.tag=objMessage.indexValue;
btnDate.titleLabel.font=[UIFont fontWithName:#"Arial" size:15];
btnDate.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnDate setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnDate];
btnSendBy = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSendBy.frame = CGRectMake(127,oldY_Axis,backgroundScroll.frame.size.width-50,200);
[btnSendBy setTitle:objMessage.senderPerson forState:UIControlStateNormal];
btnSendBy.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnSendBy.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnSendBy.tag=objMessage.indexValue;
// btnURL.backgroundColor=[UIColor redColor];
//btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnSendBy.titleLabel.font=[UIFont fontWithName:#"Arial" size:15];
btnSendBy.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnSendBy setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnSendBy];
btnSendTo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSendTo.frame = CGRectMake(11,oldY_Axis+25,backgroundScroll.frame.size.width-50,200);
[btnSendTo setTitle:objMessage.senderPerson forState:UIControlStateNormal];
btnSendTo.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnSendTo.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
// btnURL.backgroundColor=[UIColor redColor];
//btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnSendTo.titleLabel.font=[UIFont fontWithName:#"Arial" size:15];
btnSendTo.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnSendTo setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnSendTo];
NSString *communityName=#"";
if([del.communityId isEqualToString:#"none"])
{
communityName=objMessage.Community;
communityName=[NSString stringWithFormat:#"%#: ",objMessage.Community];
}
//NSLog(#"Group type %# and %#",objMessage.groupType,del.communityId);
if([objMessage.groupType isEqualToString:#"Personal"])
{
[btnSendTo setTitle:[NSString stringWithFormat:#"%#%#",communityName,objMessage.Person] forState: UIControlStateNormal];
}
else if([objMessage.groupType isEqualToString:#"Everyone"])
{
[btnSendTo setTitle:[NSString stringWithFormat:#"%#%#",communityName,#"Everyone"] forState: UIControlStateNormal];
}
else
{
[btnSendTo setTitle:[NSString stringWithFormat:#"%#%#",communityName,objMessage.GroupName] forState: UIControlStateNormal];
}
[btnURL addTarget:self action:#selector(getLink:) forControlEvents:UIControlEventTouchUpInside];
[btnAttachmentName addTarget:self action:#selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside];
[btnAttachment addTarget:self action:#selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside];
backgrounf_View.frame=CGRectMake(24,yAxis,backgroundScroll.frame.size.width-30,oldY_Axis+55);
LastMessageYaxis=backgrounf_View.frame.origin.y+backgrounf_View.frame.size.height+10;
del.RefreshValue=0;
LastValue=index;
communityObject *com=[[communityObject alloc]init];
com.MessageId=nil;
com.Date=nil;
com.Message=nil;
com.Link=nil;
com.Attachment=nil;
com.Filename=nil;
com.attachmentType=nil;
com.imageSize=nil;
com.groupType=nil;
com.Person=nil;
com.GroupName=nil;
com.senderPerson=nil;
com.Community=nil;
com.inorout=nil;
return backgrounf_View;
}
Sceenshot is added please check.
This bit looks like a mutation of array while enumerating it:
for (UIView * view in backgroundScroll.subviews) {
if (view.tag == 15) {
[view removeFromSuperview];
}
}
There is no wonder why the array doesn't like it. I would crash too :)
BTW: That part where it is storing indexes of array in view's tag seems very wrong.

Undeclared identifier "viewDidLoad"

//Objects list
//objects scroll
UIScrollView *objects;
objects=[[UIScrollView alloc]initWithFrame:CGRectMake(0,80,512,688)];
objects.showsVerticalScrollIndicator=YES;
objects.scrollEnabled=YES;
objects.userInteractionEnabled=YES;
objects.backgroundColor = [UIColor clearColor];
[self.view addSubview:objects];
[objects setUserInteractionEnabled:YES];
[objects setCanCancelContentTouches:YES];
objects.contentSize = CGSizeMake(512,689);
//divider
UIButton *divider = [UIButton buttonWithType:UIButtonTypeCustom];
[divider setBackgroundImage:[UIImage imageNamed:#"white.png"]forState:UIControlStateNormal];
divider.frame = CGRectMake(300, 0, 1, 768);
[divider setTitle:#"" forState:UIControlStateNormal];
[self.view addSubview:divider];
divider.adjustsImageWhenHighlighted = NO;
//array colors
UIImage *color[3000];
//color setup
color[0] = [UIImage imageNamed:#"item.png"];
UIImageView *originalImageView = [[UIImageView alloc] initWithImage:color[0]];
[originalImageView setFrame:CGRectMake(300, 0, 212, 62)];
[objects addSubview:originalImageView];
//array buttons
UIImageView *button[3000];
//button setup
button[0] = [[UIView alloc] initWithFrame:[originalImageView frame]];
UIImageView *maskImageView = [[UIImageView alloc] initWithImage:color[0]];
[maskImageView setFrame:[button[0] bounds]];
[[button[0] layer] setMask:[maskImageView layer]];
button[0].backgroundColor = [UIColor blueColor];
[objects addSubview:button[0]];
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the updated code, some before, and everything after. There's a lot more to the program but it is all just setting up different visual elements for a GUI. The error is tagged on the line that sais..
-(void)viewDidLoad {
And the error sais this..
Use of undeclared identifier 'viewDidLoad'
Would you like more info??
you cannot have method inside another method.
this is part of your code
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}
} // <----this should not be here
change it to
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
} // <--- should be here to end method
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}

UIElements in subclass with uiscrollview are non responsive

I have a class that provides a custom menu for all its subclasses, but since i started using the text field have run to the keyboard problem so i use the normal method and embed the subclass in a resizing uiscrollview. the problem is that the scrollview doesnt cover the menu that belongs to its super class.
The idea that came to mind is to create a scrollview in the super class and inside the scrollview put the menu. I run it and everything seemed to be okay until i couldn't interact with anything on the screen other than the navigation bar and the menu which lead me to believe that maybe the subclass uielements are not subviews of the scrollview.
The problem even expands since in the subclasses I create uielements in the storyboard and in code. My question would be how to make that all uielements created in the subclass and added in self.view become subviews of the superclass' scrollview.
SUPERCLASS
- (void)viewDidLoad
{
[super viewDidLoad];
//_scrollViewMain = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//[self.view addSubview:_scrollViewMain];
_slideButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(slide)];
[self.navigationItem setRightBarButtonItem:_slideButton];
_menuView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - 44, self.view.bounds.size.width, 44)];
_menuView.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:63.0/255.0 blue:63.0/255.0 alpha:1.0];
[self.view addSubview:_menuView];
//[_scrollViewMain addSubview:_menuView];
UIView *redFrameView = [[UIView alloc] initWithFrame:CGRectMake(0, _menuView.frame.size.height / 2, _menuView.frame.size.width, _menuView.frame.size.height / 2)];
[redFrameView setBackgroundColor:[UIColor colorWithRed:170.0/255.0 green:0.0 blue:0.0 alpha:0.5]];
[_menuView addSubview:redFrameView];
_menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_menuButton setImage:[UIImage imageNamed:#"19-gear.png"] forState:UIControlStateNormal];
[_menuButton addTarget:self action:#selector(menu) forControlEvents:UIControlEventTouchUpInside];
_menuButton.frame = CGRectMake(16.0, 9.0, 26.0, 26.0);
_addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_addButton setImage:[UIImage imageNamed:#"13-plus.png"] forState:UIControlStateNormal];
[_addButton addTarget:self action:#selector(add) forControlEvents:UIControlEventTouchUpInside];
_addButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_addButton.userInteractionEnabled = NO;
_addButton.alpha = 0.0;
_editButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_editButton setImage:[UIImage imageNamed:#"187-pencil.png"] forState:UIControlStateNormal];
[_editButton addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
_editButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_editButton.userInteractionEnabled = NO;
_editButton.alpha = 0.0;
_deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_deleteButton setImage:[UIImage imageNamed:#"11-x.png"] forState:UIControlStateNormal];
[_deleteButton addTarget:self action:#selector(delete) forControlEvents:UIControlEventTouchUpInside];
_deleteButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_deleteButton.userInteractionEnabled = NO;
_deleteButton.alpha = 0.0;
_downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_downloadButton setImage:[UIImage imageNamed:#"57-download.png"] forState:UIControlStateNormal];
[_downloadButton addTarget:self action:#selector(download) forControlEvents:UIControlEventTouchUpInside];
_downloadButton.frame = CGRectMake(16.0, 15.0, 11.0, 14.0);
_downloadButton.userInteractionEnabled = NO;
_downloadButton.alpha = 0.0;
_uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_uploadButton setImage:[UIImage imageNamed:#"56-cloud.png"] forState:UIControlStateNormal];
[_uploadButton addTarget:self action:#selector(upload) forControlEvents:UIControlEventTouchUpInside];
_uploadButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
_uploadButton.userInteractionEnabled = NO;
_uploadButton.alpha = 0.0;
_shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_shareButton setImage:[UIImage imageNamed:#"124-bullhorn.png"] forState:UIControlStateNormal];
[_shareButton addTarget:self action:#selector(share) forControlEvents:UIControlEventTouchUpInside];
_shareButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
_shareButton.userInteractionEnabled = NO;
_shareButton.alpha = 0.0;
[_menuView addSubview:_addButton];
[_menuView addSubview:_editButton];
[_menuView addSubview:_deleteButton];
[_menuView addSubview:_downloadButton];
[_menuView addSubview:_uploadButton];
[_menuView addSubview:_shareButton];
[_menuView addSubview:_menuButton];
_isMenuVisible = NO;
}
FIRST attempt but the scrolldidn't included the menuview in the superclass
- (void)viewDidLoad
{
[super viewDidLoad];
[self registerForKeyboardNotifications];
self.title = [_exercise valueForKey:kExerciseName];
[super ableAdd];
[super ableDownload];
[super ableShare];
if ([[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
[super ableUpload];
[super ableDelete];
}
_doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
_nameLabel.text = [_exercise valueForKey:kExerciseName];
_nameLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_categoryLabel.text = [[_exercise valueForKey:kExerciseEType] valueForKey:kExerciseTypeName];
_categoryLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_gearLabel.text = [[_exercise valueForKey:kGear] valueForKey:kGearName];
_gearLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_addRecordButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 30) / 2, 7, 30, 30)];
[_addRecordButton setImage:[UIImage imageNamed:#"85-trophy.png"] forState:UIControlStateNormal];
[_addRecordButton addTarget:self action:#selector(addRecord) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:_addRecordButton];
_showChartButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 7, 30, 30)];
[_showChartButton setImage:[UIImage imageNamed:#"16-line-chart.png.png"] forState:UIControlStateNormal];
[_showChartButton addTarget:self action:#selector(showChart) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:_showChartButton];
_numberOfPages = 3;
_currentPage = 1;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 174, 320, self.view.bounds.size.height - 262)];
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * _numberOfPages, _scrollView.bounds.size.height);
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = NO;
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_textView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
_textView.text = [_exercise valueForKey:kExerciseOverview];
_textView.editable = NO;
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_tableView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
_tableView.dataSource = self;
_tableView.delegate = self;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 2.0;
longPress.delegate = (id<UIGestureRecognizerDelegate>)self;
[_tableView addGestureRecognizer:longPress];
_movieView = [[UIWebView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 2, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_movieView.opaque = YES;
_movieView.backgroundColor = [UIColor whiteColor];
_movieView.autoresizesSubviews = YES;
[_movieView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[_exercise valueForKey:kExerciseUrlAddress]]]];
[_scrollView addSubview:_textView];
[_scrollView addSubview:_tableView];
[_scrollView addSubview:_movieView];
[_scrollView scrollRectToVisible:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height) animated:NO];
[_scrollViewMain addSubview:_scrollView];
//[self.view addSubview:_scrollView];
_pageControl.numberOfPages = _numberOfPages;
_pageControl.currentPage = _currentPage;
_records = [[NSMutableArray alloc] init];
_records = [[NSArray arrayWithArray:[[_exercise valueForKey:kExerciseERecords] allObjects]] mutableCopy];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:kExerciseRecordRecordDate ascending:NO];
[_records sortUsingDescriptors:[NSArray arrayWithObject:sort]];
}
- (void)viewDidDisappear:(BOOL)animated
{
if (self.editing) {
[super setPan];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[_tableView setEditing:editing animated:animated];
[super setPan];
if (self.navigationItem.rightBarButtonItem == nil) {
self.navigationItem.rightBarButtonItem = _doneButton;
}
_addRecordButton.enabled = !editing;
_showChartButton.enabled = !editing;
if (![[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
[super ableUpload];
[super ableDelete];
_textView.editable = editing;
}
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollViewMain.contentInset = contentInsets;
_scrollViewMain.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, _activeField.frame.origin.y-kbSize.height);
[_scrollViewMain setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollViewMain.contentInset = contentInsets;
_scrollViewMain.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
_activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
_activeField = nil;
}

iPad popover presentpopoverfrombarbuttonitem

I have added a few buttons to the right side of the navigation bar with the following:
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor clearColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"toc.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(tableOfContentsAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"bookmark.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:#selector(bookmarkButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[customView release];
[segmentBarItem release];
This works well. For both buttons I show a popOver as shown below
- (void) bookmarkButtonAction
{
BookmarksViewController* content = [[BookmarksViewController alloc] initWithOrientation:lastOrientation selectedPage:selectedPage];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:content];
CGSize size = content.view.frame.size;
aPopover.popoverContentSize = size;
aPopover.delegate = self;
self.bookmarksPopoverVC = content;
self.bookmarksPopoverVC.popUpController = aPopover;
[content release];
[aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[aPopover release];
bookmarksShowing = YES;
}
The problem is that I am using presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem and this shows the top arrow in the middle of the two buttons. How can I attach the arrow to each button?
instead of using this line:
aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem
You may better try this line:
aPopover presentPopoverFromBarButtonItem:sender
I think that would solve your problem
try this:
- (IBAction)products:(id)sender {
UIButton* btn = (UIButton *)sender;
[productsPopover presentPopoverFromRect:[btn bounds] inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Works like a charm

Resources